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

Overriding ToString

Scheduled Pinned Locked Moved C#
11 Posts 6 Posters 2 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.
  • M monrobot13

    I'm adding objects of a custom class to a check list box and when I call the Add () method of the check list box the string it adds is simply namespace.classname. I don't want it to add this I want it to add a string variable that is declared in the class. I tried overriding the ToString () method because I thought it might be calling that, but it still added the same thing. Does anyone know what method I can override so that it adds the string I want. Thanks again. - monrobot13

    S Offline
    S Offline
    Stephane Rodriguez
    wrote on last edited by
    #2

    Why don't you add that string to the list box instead of adding the object that contains it?

    M 1 Reply Last reply
    0
    • M monrobot13

      I'm adding objects of a custom class to a check list box and when I call the Add () method of the check list box the string it adds is simply namespace.classname. I don't want it to add this I want it to add a string variable that is declared in the class. I tried overriding the ToString () method because I thought it might be calling that, but it still added the same thing. Does anyone know what method I can override so that it adds the string I want. Thanks again. - monrobot13

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #3

      You need to call ToString() before adding it to the Items collection. I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

      M 1 Reply Last reply
      0
      • S Stephane Rodriguez

        Why don't you add that string to the list box instead of adding the object that contains it?

        M Offline
        M Offline
        monrobot13
        wrote on last edited by
        #4

        At first I didn't want to do that becasue I was going to add the whole oject to the list box. I've decided to go a different route since. Now I'm doing exactly that (simply adding the string I wanted) I'm adding the objects into an ArrayList. Thanks - monrobot13

        S 1 Reply Last reply
        0
        • L leppie

          You need to call ToString() before adding it to the Items collection. I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

          M Offline
          M Offline
          monrobot13
          wrote on last edited by
          #5

          I've decided to go a different route (see post above), but thanks for the help anyways. - monrobot13

          1 Reply Last reply
          0
          • M monrobot13

            At first I didn't want to do that becasue I was going to add the whole oject to the list box. I've decided to go a different route since. Now I'm doing exactly that (simply adding the string I wanted) I'm adding the objects into an ArrayList. Thanks - monrobot13

            S Offline
            S Offline
            se99ts
            wrote on last edited by
            #6

            If you did want to go back to your original idea... derive your object from ListViewItem (im doing it with tree's so you might need to check that), and then set its text (no need to overide) to whatever text you want to appear in the list/tree. Then when you add it to the list it will have the exact text that you want. Hope that helps any one else trying something like this! On a similary note... Does anyone know how i can add the same TreeViewItem to 2 different TreeView's without having to clone it?

            S 1 Reply Last reply
            0
            • S se99ts

              If you did want to go back to your original idea... derive your object from ListViewItem (im doing it with tree's so you might need to check that), and then set its text (no need to overide) to whatever text you want to appear in the list/tree. Then when you add it to the list it will have the exact text that you want. Hope that helps any one else trying something like this! On a similary note... Does anyone know how i can add the same TreeViewItem to 2 different TreeView's without having to clone it?

              S Offline
              S Offline
              Stephane Rodriguez
              wrote on last edited by
              #7

              se99ts wrote: derive your object from ListViewItem That's good as a solution to the original issue, although that's questionable in terms of object oriented model : if the object is non GUI related, why should a non GUI object be "attached" to a GUI class ?

              S 1 Reply Last reply
              0
              • S Stephane Rodriguez

                se99ts wrote: derive your object from ListViewItem That's good as a solution to the original issue, although that's questionable in terms of object oriented model : if the object is non GUI related, why should a non GUI object be "attached" to a GUI class ?

                S Offline
                S Offline
                se99ts
                wrote on last edited by
                #8

                i know it is a dodgy area, but i found it easier just to add the objects to a list, instead of having to store the entries in an array and then just a text representation of them in the GUI, it just meant i didnt have to do any lookup's i could just call the events on the object. though at this stage i may well have to go to a text representation as the items cant be added to 2 lists, so i may well be doing lookup's!

                S 1 Reply Last reply
                0
                • S se99ts

                  i know it is a dodgy area, but i found it easier just to add the objects to a list, instead of having to store the entries in an array and then just a text representation of them in the GUI, it just meant i didnt have to do any lookup's i could just call the events on the object. though at this stage i may well have to go to a text representation as the items cant be added to 2 lists, so i may well be doing lookup's!

                  S Offline
                  S Offline
                  Stephane Rodriguez
                  wrote on last edited by
                  #9

                  se99ts wrote: though at this stage i may well have to go to a text representation as the items cant be added to 2 lists Exactly, items are always attached to a unique control parent.

                  1 Reply Last reply
                  0
                  • M monrobot13

                    I'm adding objects of a custom class to a check list box and when I call the Add () method of the check list box the string it adds is simply namespace.classname. I don't want it to add this I want it to add a string variable that is declared in the class. I tried overriding the ToString () method because I thought it might be calling that, but it still added the same thing. Does anyone know what method I can override so that it adds the string I want. Thanks again. - monrobot13

                    W Offline
                    W Offline
                    Wolfgang Loder
                    wrote on last edited by
                    #10

                    Coming back to the original problem, you can also fill a listbox or combobox by setting the datasource to any object that implements the interface IList. I had the problem,that I wanted the ValueMember property differently set to the DisplayMember. With the collections Add() method this is not possible. So I defined a simple class: internal class clsName { private string _longname; private int _uid; public string LongName {get {return _longname;} set {_longname = value;}} public int UID {get {return _uid;} set {_uid = value;}} public clsName (string lname, int uid) { _longname = lname; _uid = uid; } } and filled the combobox with an arraylist (the parameters come from a xml file): [...] _arrNames.Add(new clsName(nav2.Value,System.Convert.ToInt32(iterator.Current.GetAttribute("UID",nav.NamespaceURI)))); [...] The combobox datasource can then be set to the arraylist and the members to the appropriate custom class properties. [...] cb.DataSource = _arrNames; cb.DisplayMember = "LongName"; cb.ValueMember = "UID"; [...] That's all and the list is filled with the strings plus the value is set to what I need in the combobox event handler later in the application. Wolfgang

                    1 Reply Last reply
                    0
                    • M monrobot13

                      I'm adding objects of a custom class to a check list box and when I call the Add () method of the check list box the string it adds is simply namespace.classname. I don't want it to add this I want it to add a string variable that is declared in the class. I tried overriding the ToString () method because I thought it might be calling that, but it still added the same thing. Does anyone know what method I can override so that it adds the string I want. Thanks again. - monrobot13

                      D Offline
                      D Offline
                      Don_s
                      wrote on last edited by
                      #11

                      There are 2 ways to do this, one of which you have already mentioned. Firstly you can use the DisplayMember property: this.checkedListBox1.DisplayMember = "Test"; // Where Test is the name of the property you would like to display Alternatively, as you mentioned, override the ToString() method. Most list type controls use this as the default DisplayMember when it is not set explicitly:

                      public override string ToString()
                      {
                      	return this.Test;
                             //Again, Where Test is the name of the property you would like to display
                      }
                      

                      I'm not sure why this did not work for you before. Both of these methods worked in a quick demo i wrote up.

                      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