Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to referenc functions in other code file

How to referenc functions in other code file

Scheduled Pinned Locked Moved C#
tutorialquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    klufy
    wrote on last edited by
    #1

    How to referenc functions in other code file/unit?

    C 1 Reply Last reply
    0
    • K klufy

      How to referenc functions in other code file/unit?

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      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

      K 1 Reply Last reply
      0
      • C Colin Angus Mackay

        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

        K Offline
        K Offline
        klufy
        wrote on last edited by
        #3

        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

        C H K 3 Replies Last reply
        0
        • K klufy

          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

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • K klufy

            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

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            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 and MyFormProject. If these are in the same assembly, they typically should share a common namespace! Notice how you keep typing using 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", BTW

            or

            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

            1 Reply Last reply
            0
            • K klufy

              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

              K Offline
              K Offline
              klufy
              wrote on last edited by
              #6

              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.

              O 1 Reply Last reply
              0
              • K klufy

                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.

                O Offline
                O Offline
                obelisk29
                wrote on last edited by
                #7

                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...

                K 1 Reply Last reply
                0
                • O obelisk29

                  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...

                  K Offline
                  K Offline
                  klufy
                  wrote on last edited by
                  #8

                  Thank you. It works.

                  H 1 Reply Last reply
                  0
                  • K klufy

                    Thank you. It works.

                    H Offline
                    H Offline
                    Heath Stewart
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups