How can I create a function like toString()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SvenV
    New Member
    • Oct 2008
    • 50

    How can I create a function like toString()

    I'm working on a class to create an xml file. I need to create this using a special way. I define my tags in different classes with a const containing the value. I do this to have a global definition of the tags.

    Now I want to create two functions to complete the tags with angle brackets. These functions should be available for every class. Something simular to toString()

    The code below was my initial thought but I need some help.
    Can anyone help me?

    Code:
    public abstract class XMLFunctions
        {
            public virtual string StartXML()
            {
                return "<"+?+">";
            }
    
            public virtual string EndXML()
            {
                return "</"+?+">";
            }
        }
    	public class XMLMsg:XMLFunctions
    	{
    		public const string TAG = "ROOTNODE";
    
    		public class HDR
    		{
                                           .............
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    I don't understand what your problem is. What's wrong with the code you posted?

    Also, standard XML doesn't have a closing </?> tag.

    Comment

    • SvenV
      New Member
      • Oct 2008
      • 50

      #3
      Where the ? should be the content of the TAG
      I want to do the following:

      XMLMSG.TAG.Star tXML();

      then I want as an outcome: <ROOTNODE>

      I'm wondering where needs to be where the ? is now

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by SvenV
        I'm wondering where needs to be where the ? is now
        What do you mean: "Where needs to be where the ? is now" ???

        If you want a function to be in every class, you have to implement that function in every class.

        You could make a base class that all of your classes derive from that contain functions that are common between the classes....

        That is how the ToString() function is in every object...every class derives from the Object class (the Object class is the base class for every class)...and the Object class has a ToString() function in it.

        -Frinny

        Comment

        • SvenV
          New Member
          • Oct 2008
          • 50

          #5
          Sorry if I haven't made myself clear enough,
          I have several different classes all with one constant value, tag.

          Now I want every class to have the following two functions

          Code:
           public virtual string StartXML()
                  {
                       return "<"+?+">";
                  }
            
                   public virtual string EndXML()
                   {
                       return "</"+?+">";
                  }
          I thought of creating an abstract class with these two functions and then let every class inherit from that abstract class. Basically the two functions create an xml valid tag using the tag constant in the class. But as you see the two methods now contain: return "<"+?+">";

          Where the question mark is now, I want the constant Tag value from the class that I'm calling. f.e.

          XmlMsg.TAG.Star txml();
          The output should be <Rootnode>

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            OK, now I understand what you want to do.

            This is an interesting one. Are you familiar with properties? Something that looks like this:
            Code:
            private int prop;
            public int Prop
            {
              get { return prop; }
              set { prop = value;}
            }
            If you aren't familiar with this, the benefits of properties are that you can do more than simply set and get their values, you can have any kind of calculation you want. Also (and this is the important one) they can be overridden.

            What you'll need to do is define a protected virtual property called "RootXML" or something like that. And you don't need your StartXML and EndXML methods as virtual. So your code should look like this:
            Code:
            protected virtual string RootXML
            {
                get { return "Root"; }
            }
            
            public string StartXML()
            {
                return String.Format("<{0}>", RootXML);
            }
            
            public string EndXML()
            {
                return String.Format("</{0}>", RootXML);
            }
            Now, in your derived class, you can do this:
            Code:
            protected override string RootXML
            {
                get { return "DerivedRoot"; }
            }
            "DerivedRoo t" can be anything.

            And that's all you need to do. Now if I do this in a console program (assuming we named the derived class we created DerivedClass):
            Code:
            DerivedClass c = new DerivedClass();
            Console.WriteLine(c.StartXML());
            Console.WriteLine(c.EndXML());
            This is the output:
            <DerivedRoot>
            </DerivedRoot>
            Hope that helps.

            Comment

            • SvenV
              New Member
              • Oct 2008
              • 50

              #7
              That's it, I know what properties are yea but didn't think of this solution..
              Thank you very much!

              Comment

              • Curtis Rutland
                Recognized Expert Specialist
                • Apr 2008
                • 3264

                #8
                Cool. Hope it didn't sound like I was talking down to you or something, but we never know what the level of knowledge people have is, so I try to make it simple.

                Comment

                Working...