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. Accessing a method in another file

Accessing a method in another file

Scheduled Pinned Locked Moved C#
questionbeta-testingxmltutorial
15 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.
  • Y Offline
    Y Offline
    Yustme
    wrote on last edited by
    #1

    Hi, I usually put all my codes in one .cs file. But im now trying a bit organized programming. I made a method in another file, an xml writer file. And i want to send commands to it from my form.cs file. So how can i access a method which is in for example alfa.cs file from beta.cs file? Both files are in the same project. Thanks in advance!

    A C 2 Replies Last reply
    0
    • Y Yustme

      Hi, I usually put all my codes in one .cs file. But im now trying a bit organized programming. I made a method in another file, an xml writer file. And i want to send commands to it from my form.cs file. So how can i access a method which is in for example alfa.cs file from beta.cs file? Both files are in the same project. Thanks in advance!

      A Offline
      A Offline
      Andrei Ungureanu
      wrote on last edited by
      #2

      Hi, The only thing that you need to do for using a class/method from another file from the same project is to use the NameSpace inside which is the method. Nothing else. Hope it helps

      Do your best to be the best

      Y 1 Reply Last reply
      0
      • Y Yustme

        Hi, I usually put all my codes in one .cs file. But im now trying a bit organized programming. I made a method in another file, an xml writer file. And i want to send commands to it from my form.cs file. So how can i access a method which is in for example alfa.cs file from beta.cs file? Both files are in the same project. Thanks in advance!

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        If a method is static, then you can call it from anywhere in your project. If it's not static, you need an instance of the class to call the methods on. Class C1 { private int n = 0; private static int s = 0; public int Int { get { return n; } } public int IntS { get { return s; } } public void Add(int i) { n += i; } public static AddS(int i) { s+=i; } } Class C2 { public DoStuff() { C1.AddS(10); Console.WriteLine(C1.IntS.ToString()); // 10 C1 c1 = new C1(); c1.Add(5); Console.WriteLine(c1.Int.ToString()); // 5 C1 c2 = new C1(); c2.Add(52); // c2.Int now = 52, c1.Int still = 5, C1.IntS still = 10 C1.AddS(10); Console.WriteLine(C1.IntS.ToString()); // 20 c1.Add(12); Console.WriteLine(c1.Int.ToString()); // 17 Console.WriteLine(c2.Int.ToString()); // 52 } } The most common mistake I've seen is for people to have one instance of a class, to be in another class where they don't currently have access to that instance, so they create a new instance ( often of a form ) and try to change variables on that. this won't ever change variables on any instance bar the new one, unless they are static.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

        Y 1 Reply Last reply
        0
        • A Andrei Ungureanu

          Hi, The only thing that you need to do for using a class/method from another file from the same project is to use the NameSpace inside which is the method. Nothing else. Hope it helps

          Do your best to be the best

          Y Offline
          Y Offline
          Yustme
          wrote on last edited by
          #4

          Hi, They all have the same namespace.

          1 Reply Last reply
          0
          • C Christian Graus

            If a method is static, then you can call it from anywhere in your project. If it's not static, you need an instance of the class to call the methods on. Class C1 { private int n = 0; private static int s = 0; public int Int { get { return n; } } public int IntS { get { return s; } } public void Add(int i) { n += i; } public static AddS(int i) { s+=i; } } Class C2 { public DoStuff() { C1.AddS(10); Console.WriteLine(C1.IntS.ToString()); // 10 C1 c1 = new C1(); c1.Add(5); Console.WriteLine(c1.Int.ToString()); // 5 C1 c2 = new C1(); c2.Add(52); // c2.Int now = 52, c1.Int still = 5, C1.IntS still = 10 C1.AddS(10); Console.WriteLine(C1.IntS.ToString()); // 20 c1.Add(12); Console.WriteLine(c1.Int.ToString()); // 17 Console.WriteLine(c2.Int.ToString()); // 52 } } The most common mistake I've seen is for people to have one instance of a class, to be in another class where they don't currently have access to that instance, so they create a new instance ( often of a form ) and try to change variables on that. this won't ever change variables on any instance bar the new one, unless they are static.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

            Y Offline
            Y Offline
            Yustme
            wrote on last edited by
            #5

            Hi, Nothing in my project is static. The method is a void one, its not returning anything (as it should). This: C1 c2 = new C1(); Helped! Thank you very much!

            C 1 Reply Last reply
            0
            • Y Yustme

              Hi, Nothing in my project is static. The method is a void one, its not returning anything (as it should). This: C1 c2 = new C1(); Helped! Thank you very much!

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              Yustme wrote:

              Nothing in my project is static.

              OK, then you need to keep a specific instance of the class you want to call, so you can work with it. If the method you're calling doesn't rely on any member variables, then it should be static.

              Yustme wrote:

              The method is a void one, its not returning anything (as it should).

              OK, that makes no sense. If it's got void, then it doesn't return anything. I think it's time you posted some code.

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

              Y 1 Reply Last reply
              0
              • C Christian Graus

                Yustme wrote:

                Nothing in my project is static.

                OK, then you need to keep a specific instance of the class you want to call, so you can work with it. If the method you're calling doesn't rely on any member variables, then it should be static.

                Yustme wrote:

                The method is a void one, its not returning anything (as it should).

                OK, that makes no sense. If it's got void, then it doesn't return anything. I think it's time you posted some code.

                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                Y Offline
                Y Offline
                Yustme
                wrote on last edited by
                #7

                Hi, I know it doenst return anything when its void, but it was more like extra info to your example where you showed me 2 methods that returned something. This is my method: public void WritingToXMLDoc(string elementString, string writeToXMLDoc) { this.writer.WriteElementString(elementString, writeToXMLDoc); this.writer.WriteEndElement(); if (this.writer != null) this.writer.Close(); }

                C 1 Reply Last reply
                0
                • Y Yustme

                  Hi, I know it doenst return anything when its void, but it was more like extra info to your example where you showed me 2 methods that returned something. This is my method: public void WritingToXMLDoc(string elementString, string writeToXMLDoc) { this.writer.WriteElementString(elementString, writeToXMLDoc); this.writer.WriteEndElement(); if (this.writer != null) this.writer.Close(); }

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #8

                  Dude, that's some strange code. You check if writer is null, after using it twice, and if it's not, you call close on it. What does close do ? Does it stop WriteElementString from working later ? If this is the object I think it is, WriteEndElement is paired with WriteStartElement, so you're using it wrong.

                  Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                  Y 1 Reply Last reply
                  0
                  • C Christian Graus

                    Dude, that's some strange code. You check if writer is null, after using it twice, and if it's not, you call close on it. What does close do ? Does it stop WriteElementString from working later ? If this is the object I think it is, WriteEndElement is paired with WriteStartElement, so you're using it wrong.

                    Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                    Y Offline
                    Y Offline
                    Yustme
                    wrote on last edited by
                    #9

                    Hi, There is actually more code. Complete class: class XMLWriter { XmlWriterSettings settings = new XmlWriterSettings(); DateTime dateTimeNow = DateTime.Now; XmlWriter writer = null; public XMLWriter() { this.settings.Indent = true; this.settings.IndentChars = "\t"; this.settings.OmitXmlDeclaration = true; this.settings.NewLineOnAttributes = true; this.settings.Encoding = Encoding.Default; this.settings.ConformanceLevel = ConformanceLevel.Fragment; this.writer = XmlWriter.Create("Testing.xml", settings); this.writer.WriteStartElement("Information"); this.writer.WriteElementString("DateOfBuilding", dateTimeNow.ToShortDateString()); this.writer.WriteElementString("TimeOfBuilding", dateTimeNow.ToLongTimeString()); } public void WritingToXMLDoc(string elementString, string writeToXMLDoc) { this.writer.WriteElementString(elementString, writeToXMLDoc); this.writer.WriteEndElement(); this.writer.Flush(); if (this.writer != null) this.writer.Close(); } } writer.Close(); is to stop writing and used for disposal.

                    M C 2 Replies Last reply
                    0
                    • Y Yustme

                      Hi, There is actually more code. Complete class: class XMLWriter { XmlWriterSettings settings = new XmlWriterSettings(); DateTime dateTimeNow = DateTime.Now; XmlWriter writer = null; public XMLWriter() { this.settings.Indent = true; this.settings.IndentChars = "\t"; this.settings.OmitXmlDeclaration = true; this.settings.NewLineOnAttributes = true; this.settings.Encoding = Encoding.Default; this.settings.ConformanceLevel = ConformanceLevel.Fragment; this.writer = XmlWriter.Create("Testing.xml", settings); this.writer.WriteStartElement("Information"); this.writer.WriteElementString("DateOfBuilding", dateTimeNow.ToShortDateString()); this.writer.WriteElementString("TimeOfBuilding", dateTimeNow.ToLongTimeString()); } public void WritingToXMLDoc(string elementString, string writeToXMLDoc) { this.writer.WriteElementString(elementString, writeToXMLDoc); this.writer.WriteEndElement(); this.writer.Flush(); if (this.writer != null) this.writer.Close(); } } writer.Close(); is to stop writing and used for disposal.

                      M Offline
                      M Offline
                      mesmer
                      wrote on last edited by
                      #10

                      If both your classes are in the same namespace, simply create an instance of your class and make a call to the method ... XMLWriter myObject = new XMLWriter(); myObject.WritingToXMLDoc("elementString", "writeToXMLDoc"); ... I'm not sure if im on the right path here but i hope it helps

                      Y 1 Reply Last reply
                      0
                      • Y Yustme

                        Hi, There is actually more code. Complete class: class XMLWriter { XmlWriterSettings settings = new XmlWriterSettings(); DateTime dateTimeNow = DateTime.Now; XmlWriter writer = null; public XMLWriter() { this.settings.Indent = true; this.settings.IndentChars = "\t"; this.settings.OmitXmlDeclaration = true; this.settings.NewLineOnAttributes = true; this.settings.Encoding = Encoding.Default; this.settings.ConformanceLevel = ConformanceLevel.Fragment; this.writer = XmlWriter.Create("Testing.xml", settings); this.writer.WriteStartElement("Information"); this.writer.WriteElementString("DateOfBuilding", dateTimeNow.ToShortDateString()); this.writer.WriteElementString("TimeOfBuilding", dateTimeNow.ToLongTimeString()); } public void WritingToXMLDoc(string elementString, string writeToXMLDoc) { this.writer.WriteElementString(elementString, writeToXMLDoc); this.writer.WriteEndElement(); this.writer.Flush(); if (this.writer != null) this.writer.Close(); } } writer.Close(); is to stop writing and used for disposal.

                        C Offline
                        C Offline
                        Christian Graus
                        wrote on last edited by
                        #11

                        Yustme wrote:

                        DateTime dateTimeNow = DateTime.Now;

                        Why ?

                        Yustme wrote:

                        writer.Close(); is to stop writing and used for disposal.

                        But that reads as if the method can only be called once - why ? What are you expecting and what are you getting ?

                        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                        Y 1 Reply Last reply
                        0
                        • C Christian Graus

                          Yustme wrote:

                          DateTime dateTimeNow = DateTime.Now;

                          Why ?

                          Yustme wrote:

                          writer.Close(); is to stop writing and used for disposal.

                          But that reads as if the method can only be called once - why ? What are you expecting and what are you getting ?

                          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                          Y Offline
                          Y Offline
                          Yustme
                          wrote on last edited by
                          #12

                          Hi, To write the date and time in the xml file too. What do you suggets instead of using Close()? Havent tested it all yet, but it writes the start and end element and the time and date.

                          C 1 Reply Last reply
                          0
                          • M mesmer

                            If both your classes are in the same namespace, simply create an instance of your class and make a call to the method ... XMLWriter myObject = new XMLWriter(); myObject.WritingToXMLDoc("elementString", "writeToXMLDoc"); ... I'm not sure if im on the right path here but i hope it helps

                            Y Offline
                            Y Offline
                            Yustme
                            wrote on last edited by
                            #13

                            Hi, Thats exactly what i have :)

                            1 Reply Last reply
                            0
                            • Y Yustme

                              Hi, To write the date and time in the xml file too. What do you suggets instead of using Close()? Havent tested it all yet, but it writes the start and end element and the time and date.

                              C Offline
                              C Offline
                              Christian Graus
                              wrote on last edited by
                              #14

                              Yustme wrote:

                              To write the date and time in the xml file too.

                              But it's more than redundant, it will continue to point to the moment the file was created, which is not what Now means. And, if you only use it in one method, why is it even a member, assuming you had to create it at all ? Just use DateTime.Now.

                              Yustme wrote:

                              What do you suggets instead of using Close()?

                              If you need to close the file, expose a method to do this, otherwise calling your other method once will close the file, but not make this clear.

                              Yustme wrote:

                              Havent tested it all yet

                              OK, then at what point did you have a problem with what it's doing ? Or did I misunderstand you ?

                              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                              Y 1 Reply Last reply
                              0
                              • C Christian Graus

                                Yustme wrote:

                                To write the date and time in the xml file too.

                                But it's more than redundant, it will continue to point to the moment the file was created, which is not what Now means. And, if you only use it in one method, why is it even a member, assuming you had to create it at all ? Just use DateTime.Now.

                                Yustme wrote:

                                What do you suggets instead of using Close()?

                                If you need to close the file, expose a method to do this, otherwise calling your other method once will close the file, but not make this clear.

                                Yustme wrote:

                                Havent tested it all yet

                                OK, then at what point did you have a problem with what it's doing ? Or did I misunderstand you ?

                                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                                Y Offline
                                Y Offline
                                Yustme
                                wrote on last edited by
                                #15

                                Hi, Not having any problems atm.

                                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