How to referenc functions in other code file
-
Thats a bit vague. Are they .NET assemblies or C code or what?
EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar "Get in touch with your Inner Capitalist - I wish you much success!" -- Christopher Duncan, Lounge 9-Feb-2004
-
Thats a bit vague. Are they .NET assemblies or C code or what?
EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar "Get in touch with your Inner Capitalist - I wish you much success!" -- Christopher Duncan, Lounge 9-Feb-2004
Sorry, I am new in .Net. I want to call a functin in c# code file from a form code in same application, I tried this: //uMyLib.c // created on 12/02/2004 at 02:16 ? using System; namespace TextLib { public class TextLib { string ReversText(string s) { string txt = ""; //for (int i=0; i <= s.Length-1; i++) for (int i = s.Length-1; i>=0 ;i--) { txt += s[i].ToString(); } return txt; } } } //============== // project created on 12/02/2004 at 02:12 Õ using System; using System.Windows.Forms; using uMyLib; // file name namespace MyFormProject { class MainForm : System.Windows.Forms.Form { ....... .......... .......... void ButtonClick(object sender, System.EventArgs e) { uMyLib.MyLib S = new uMyLib.TextLib(); label.Text = S.ReversText(textBox.Text); } Thanks
-
Sorry, I am new in .Net. I want to call a functin in c# code file from a form code in same application, I tried this: //uMyLib.c // created on 12/02/2004 at 02:16 ? using System; namespace TextLib { public class TextLib { string ReversText(string s) { string txt = ""; //for (int i=0; i <= s.Length-1; i++) for (int i = s.Length-1; i>=0 ;i--) { txt += s[i].ToString(); } return txt; } } } //============== // project created on 12/02/2004 at 02:12 Õ using System; using System.Windows.Forms; using uMyLib; // file name namespace MyFormProject { class MainForm : System.Windows.Forms.Form { ....... .......... .......... void ButtonClick(object sender, System.EventArgs e) { uMyLib.MyLib S = new uMyLib.TextLib(); label.Text = S.ReversText(textBox.Text); } Thanks
klufy wrote: using uMyLib; // file name This should not be the name of the file, but the name of the namespace. So rewrite it as:
using TextLib;
Also, is this file in the same project or another project? If it is in the same project then there is nothing more you should need to do. If it is in another project is the other project in the same solution as this one or not? If the two projects are in the same solution right click the forms project (which is probably your exe file) and select "Add Reference..." from the context menu, in the dialog select the projects tab and select the project that includes TextLib. If the project containing TextLib is not in the solution it may be best to add it to the solution (if they are both under your control) or just link to it if not. To add the other project right-click on the root element in the Solution Explorer (the solution element) and select "Add"-->"Existing Project..." and browse for the project file. Does this help?
EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar "Get in touch with your Inner Capitalist - I wish you much success!" -- Christopher Duncan, Lounge 9-Feb-2004
-
Sorry, I am new in .Net. I want to call a functin in c# code file from a form code in same application, I tried this: //uMyLib.c // created on 12/02/2004 at 02:16 ? using System; namespace TextLib { public class TextLib { string ReversText(string s) { string txt = ""; //for (int i=0; i <= s.Length-1; i++) for (int i = s.Length-1; i>=0 ;i--) { txt += s[i].ToString(); } return txt; } } } //============== // project created on 12/02/2004 at 02:12 Õ using System; using System.Windows.Forms; using uMyLib; // file name namespace MyFormProject { class MainForm : System.Windows.Forms.Form { ....... .......... .......... void ButtonClick(object sender, System.EventArgs e) { uMyLib.MyLib S = new uMyLib.TextLib(); label.Text = S.ReversText(textBox.Text); } Thanks
This is fundamental to object-oriented programming and you really should pick up a good book about the .NET Framework (which most entry-level books present information on basic OO designs and programming) from http://www.microsoft.com/mspress[^] or something. First of all, the files DO NOT matter, so long as their compiled into the same assembly (except in the case of Java, in which only one public class can be in a file and the classname and filename must match, and they get compiled to .class files). Second, you don't reference assembly names in code - the current assembly references other assemblies. In your case, you're using two completely different namespaces,
TextLib
andMyFormProject
. If these are in the same assembly, they typically should share a common namespace! Notice how you keep typingusing System._Something_
? That's not magic - you're merely telling the compiler which namespaces - which can span multiple assemblies - to look in for classes and other Types. The filename matters not. So, you can do either of the following:using TextLib; // The namespace
// ...
TextLib tl = new TextLib(); // The class
label.Text = tl.ReversText(textBox.Text); // That's "Reverse" with an "e", BTWor
TextLib.TextLib tl = new TextLib.TextLib(); // The namespace.classname
label.Text = tl.ReversText(textbox.Text);Again, you really should read a book or two on .NET programming.
Microsoft MVP, Visual C# My Articles
-
Sorry, I am new in .Net. I want to call a functin in c# code file from a form code in same application, I tried this: //uMyLib.c // created on 12/02/2004 at 02:16 ? using System; namespace TextLib { public class TextLib { string ReversText(string s) { string txt = ""; //for (int i=0; i <= s.Length-1; i++) for (int i = s.Length-1; i>=0 ;i--) { txt += s[i].ToString(); } return txt; } } } //============== // project created on 12/02/2004 at 02:12 Õ using System; using System.Windows.Forms; using uMyLib; // file name namespace MyFormProject { class MainForm : System.Windows.Forms.Form { ....... .......... .......... void ButtonClick(object sender, System.EventArgs e) { uMyLib.MyLib S = new uMyLib.TextLib(); label.Text = S.ReversText(textBox.Text); } Thanks
Thanks Colin and Heath. Boath files are in the same project. My call now is like this: void ButtonClick(object sender, System.EventArgs e) { TextLib.TextLib tl = new TextLib.TextLib(); label.Text = tl.ReverseText(textBox.Text); } I got this error : 'TextLib.TextLib.ReverseText(string)' is inaccessible due to its protection level(CS0122) Thank you again for your advice.
-
Thanks Colin and Heath. Boath files are in the same project. My call now is like this: void ButtonClick(object sender, System.EventArgs e) { TextLib.TextLib tl = new TextLib.TextLib(); label.Text = tl.ReverseText(textBox.Text); } I got this error : 'TextLib.TextLib.ReverseText(string)' is inaccessible due to its protection level(CS0122) Thank you again for your advice.
On your ReverseText Function put it as
public string ReverseText(string s)
{
//code here
}This is because the C# Compiler marks it as private(thus only allowing it to be accessed by the same class it's in) if you don't specify an access modifier. Other modifiers are protected virtual abstract private public Look up these modifiers on codeproject for more info =) ------------------ I'm naked under my clothes...
-
On your ReverseText Function put it as
public string ReverseText(string s)
{
//code here
}This is because the C# Compiler marks it as private(thus only allowing it to be accessed by the same class it's in) if you don't specify an access modifier. Other modifiers are protected virtual abstract private public Look up these modifiers on codeproject for more info =) ------------------ I'm naked under my clothes...
-
Actually, lookup information on access modifiers here: 3.5.1 Declared Accessibility[^] in the C# language specification on MSDN. In fact, you'd do well to read the whole specification.
Microsoft MVP, Visual C# My Articles