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. Overriding ToString() with arguments

Overriding ToString() with arguments

Scheduled Pinned Locked Moved C#
question
11 Posts 5 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.
  • J Offline
    J Offline
    JoeRip
    wrote on last edited by
    #1

    I have a struct:

    public struct IITObjectPersistentID
    {
    public int highID;
    public int lowID;

            public IITObjectPersistentID(int HighID, int LowID)
            {
                highID = HighID;
                lowID = LowID;
            }
    
            public override string ToString()
            {
                return "{" + highID.ToString() + "." + lowID.ToString() + "}";
            }
        }
    

    I have overridden ToString(), but I want to be able to specify the normal ToString() arguments. Ie, I want to be able to get the hex display of the numbers, too, like:

    IITObjectPersistentID OPIDx = new IITObjectPersistentID(25,50);
    string displayAsHex = OPIDx.ToString("X");

    How can I write conditional code in the override statement that lets me pass arguments to OPIDx.ToString() ?

    P D B L 4 Replies Last reply
    0
    • J JoeRip

      I have a struct:

      public struct IITObjectPersistentID
      {
      public int highID;
      public int lowID;

              public IITObjectPersistentID(int HighID, int LowID)
              {
                  highID = HighID;
                  lowID = LowID;
              }
      
              public override string ToString()
              {
                  return "{" + highID.ToString() + "." + lowID.ToString() + "}";
              }
          }
      

      I have overridden ToString(), but I want to be able to specify the normal ToString() arguments. Ie, I want to be able to get the hex display of the numbers, too, like:

      IITObjectPersistentID OPIDx = new IITObjectPersistentID(25,50);
      string displayAsHex = OPIDx.ToString("X");

      How can I write conditional code in the override statement that lets me pass arguments to OPIDx.ToString() ?

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      You don't. You just overload ToString by writing a new ToString method that takes the argument. Just as int does.

      public string
      ToString
      (
      string Format
      )
      {
      return "{" + highID.ToString ( Format ) + "." + lowID.ToString ( Format ) + "}" ;
      }

      J 1 Reply Last reply
      0
      • J JoeRip

        I have a struct:

        public struct IITObjectPersistentID
        {
        public int highID;
        public int lowID;

                public IITObjectPersistentID(int HighID, int LowID)
                {
                    highID = HighID;
                    lowID = LowID;
                }
        
                public override string ToString()
                {
                    return "{" + highID.ToString() + "." + lowID.ToString() + "}";
                }
            }
        

        I have overridden ToString(), but I want to be able to specify the normal ToString() arguments. Ie, I want to be able to get the hex display of the numbers, too, like:

        IITObjectPersistentID OPIDx = new IITObjectPersistentID(25,50);
        string displayAsHex = OPIDx.ToString("X");

        How can I write conditional code in the override statement that lets me pass arguments to OPIDx.ToString() ?

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        You don't need to override ToString if your passing arguments. Every object has a default ToString() with no parameters, which will return the objects name. If you need your own paramaterless ToString then you use override to override the default method with the same signature.

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

        J 1 Reply Last reply
        0
        • D DaveyM69

          You don't need to override ToString if your passing arguments. Every object has a default ToString() with no parameters, which will return the objects name. If you need your own paramaterless ToString then you use override to override the default method with the same signature.

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

          J Offline
          J Offline
          JoeRip
          wrote on last edited by
          #4

          I'm not following you. My struct has two fields. The default ToString() simply returns the name of the struct. I want my ToString to either return the format I provided {122345.67879} or, say, the same fields in hex {FFFFF.00000}. (yes I realize that's not proper hex) So how do I write my ToString override to accept an argument (like "X")?

          D 1 Reply Last reply
          0
          • P PIEBALDconsult

            You don't. You just overload ToString by writing a new ToString method that takes the argument. Just as int does.

            public string
            ToString
            (
            string Format
            )
            {
            return "{" + highID.ToString ( Format ) + "." + lowID.ToString ( Format ) + "}" ;
            }

            J Offline
            J Offline
            JoeRip
            wrote on last edited by
            #5

            I'm sorry, your code confuses me. Are you saying I just need to remove the "override" portion of my method?

            J 1 Reply Last reply
            0
            • J JoeRip

              I'm not following you. My struct has two fields. The default ToString() simply returns the name of the struct. I want my ToString to either return the format I provided {122345.67879} or, say, the same fields in hex {FFFFF.00000}. (yes I realize that's not proper hex) So how do I write my ToString override to accept an argument (like "X")?

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              Voting 1 because you don't understand isn't going to get you very far on these forums! I'm in a good mood today however so how about something like this?

              public override string ToString()
              {
              return ToString(string.Empty);
              }
              public string ToString(string format)
              {
              return "{" + highID.ToString(format) + "." + lowID.ToString(format) + "}";
              }

              Dave
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

              J 1 Reply Last reply
              0
              • D DaveyM69

                Voting 1 because you don't understand isn't going to get you very far on these forums! I'm in a good mood today however so how about something like this?

                public override string ToString()
                {
                return ToString(string.Empty);
                }
                public string ToString(string format)
                {
                return "{" + highID.ToString(format) + "." + lowID.ToString(format) + "}";
                }

                Dave
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                J Offline
                J Offline
                JoeRip
                wrote on last edited by
                #7

                Thanks - apparently the fix is not including the "override" declaration in the second ToString() method. This was preventing it from compiling.

                1 Reply Last reply
                0
                • J JoeRip

                  I'm sorry, your code confuses me. Are you saying I just need to remove the "override" portion of my method?

                  J Offline
                  J Offline
                  JoeRip
                  wrote on last edited by
                  #8

                  You do appear to be saying that, then, as it worked! Thanks. So for any subsequent readers, the answer is: You write the first ToString method as "override" and without parameters - then you write more ToString() overloaded methods without the "override" conditional/declaration/whatever that part of the signature is. So:

                      public struct IITObjectPersistentID
                      {
                          public int highID;
                          public int lowID;
                  
                          public IITObjectPersistentID(int HighID, int LowID)
                          {
                              highID = HighID;
                              lowID = LowID;
                          }
                  
                          public override string ToString()
                          {
                              return "{" + highID.ToString() + "." + lowID.ToString() + "}";
                          }
                  
                          public string ToString(string Format)
                          {
                              return "{" + highID.ToString(Format) + "." + lowID.ToString(Format) + "}";
                          }
                      }
                  
                  P 1 Reply Last reply
                  0
                  • J JoeRip

                    I have a struct:

                    public struct IITObjectPersistentID
                    {
                    public int highID;
                    public int lowID;

                            public IITObjectPersistentID(int HighID, int LowID)
                            {
                                highID = HighID;
                                lowID = LowID;
                            }
                    
                            public override string ToString()
                            {
                                return "{" + highID.ToString() + "." + lowID.ToString() + "}";
                            }
                        }
                    

                    I have overridden ToString(), but I want to be able to specify the normal ToString() arguments. Ie, I want to be able to get the hex display of the numbers, too, like:

                    IITObjectPersistentID OPIDx = new IITObjectPersistentID(25,50);
                    string displayAsHex = OPIDx.ToString("X");

                    How can I write conditional code in the override statement that lets me pass arguments to OPIDx.ToString() ?

                    B Offline
                    B Offline
                    BhadeliaImran
                    wrote on last edited by
                    #9

                    You just need to crate one more method in you structure with ToString which contains single argument. like

                    public struct IITObjectPersistentID {
                    public int highID;
                    public int lowID;
                    public IITObjectPersistentID(int HighID, int LowID)
                    {
                    highID = HighID;
                    lowID = LowID;
                    }
                    public override string ToString()
                    {
                    return "{" + highID.ToString() + "." + lowID.ToString() + "}";
                    }

                    internal string ToString(string str)
                    {
                        //Do ur code here
                    }
                    

                    }

                    1 Reply Last reply
                    0
                    • J JoeRip

                      I have a struct:

                      public struct IITObjectPersistentID
                      {
                      public int highID;
                      public int lowID;

                              public IITObjectPersistentID(int HighID, int LowID)
                              {
                                  highID = HighID;
                                  lowID = LowID;
                              }
                      
                              public override string ToString()
                              {
                                  return "{" + highID.ToString() + "." + lowID.ToString() + "}";
                              }
                          }
                      

                      I have overridden ToString(), but I want to be able to specify the normal ToString() arguments. Ie, I want to be able to get the hex display of the numbers, too, like:

                      IITObjectPersistentID OPIDx = new IITObjectPersistentID(25,50);
                      string displayAsHex = OPIDx.ToString("X");

                      How can I write conditional code in the override statement that lets me pass arguments to OPIDx.ToString() ?

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      Hi, there are no optional parameters in C#; each combination of parameters results in another method. ToString() and ToString(string) are two different methods that share their name, but not their signature (=list of parameter types). Object class has a ToString() but not a ToString(string). So when you class derives from Object, it needs an override to provide its own ToString() and it is not allowed an override keyword on ToString(string). :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      Voting for dummies? No thanks. X|


                      1 Reply Last reply
                      0
                      • J JoeRip

                        You do appear to be saying that, then, as it worked! Thanks. So for any subsequent readers, the answer is: You write the first ToString method as "override" and without parameters - then you write more ToString() overloaded methods without the "override" conditional/declaration/whatever that part of the signature is. So:

                            public struct IITObjectPersistentID
                            {
                                public int highID;
                                public int lowID;
                        
                                public IITObjectPersistentID(int HighID, int LowID)
                                {
                                    highID = HighID;
                                    lowID = LowID;
                                }
                        
                                public override string ToString()
                                {
                                    return "{" + highID.ToString() + "." + lowID.ToString() + "}";
                                }
                        
                                public string ToString(string Format)
                                {
                                    return "{" + highID.ToString(Format) + "." + lowID.ToString(Format) + "}";
                                }
                            }
                        
                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #11

                        Correct, the term to remember here is "overload".

                        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