Overiding ToString() Method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • truezplaya
    New Member
    • Jul 2007
    • 115

    Overiding ToString() Method

    I have my collectionOfCar s and my car class.

    In my car class i have written


    Code:
    public override string ToString()
    {
      return String.Format(_carColour + " " + _carMake);
    }
    I am trying to write these cars to a listView

    Code:
    private void FillCarLists(ListView lvi, CollectionOfCars Cars)
    {
      foreach (Car myCar in CollectionOfCars )
      {
        ListViewItem test= new ListViewItem(myCar.ToString());
        lvi.Items.Add(test);
      }
    
    }
    this returns me
    "carApp.Car "

    This isn't the desired outcome i gather that the ToString() override isn't working and i have no idea why. AnyOne have any ideas?

    i'm sure it's simple but it is just bugging me now

    thanks in advanced

    Cheers

    Truez
    Last edited by tlhintoq; Dec 21 '09, 03:32 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Just for reference... I do something similar in my plugins:
      Code:
              public override string ToString()
              {
                  return string.Format("{0}: {1}", GetTypecharFromEnum(myType), myPath);
              }
      My only comment is how you are formatting.
      Code:
      return String.Format(_carColour + " " + _carMake);
      Really doesn't make use of the String.Format in any way.

      You could just as easily
      Code:
      return _carColour + " " + _carMake;
      or if you want to use String.Format then:
      Code:
      return String.Format( "{0} {1}", _carColour, _carMake);
      I would also suggest that you put a breakpoint on your return line. If you code doesn't break, then you know you are not executing this line.

      Comment

      • truezplaya
        New Member
        • Jul 2007
        • 115

        #4
        I found the issue, Since i am new to c#.net the namespaces have got jumbled up which was hiding the ToString override

        Cheers for the replys

        Truez

        Comment

        Working...