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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  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 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