473,473 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Overiding ToString() Method

115 New Member
I have my collectionOfCars and my car class.

In my car class i have written


Expand|Select|Wrap|Line Numbers
  1. public override string ToString()
  2. {
  3.   return String.Format(_carColour + " " + _carMake);
  4. }
I am trying to write these cars to a listView

Expand|Select|Wrap|Line Numbers
  1. private void FillCarLists(ListView lvi, CollectionOfCars Cars)
  2. {
  3.   foreach (Car myCar in CollectionOfCars )
  4.   {
  5.     ListViewItem test= new ListViewItem(myCar.ToString());
  6.     lvi.Items.Add(test);
  7.   }
  8.  
  9. }
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
Dec 21 '09 #1
3 1408
tlhintoq
3,525 Recognized Expert Specialist
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.
Dec 21 '09 #2
tlhintoq
3,525 Recognized Expert Specialist
Just for reference... I do something similar in my plugins:
Expand|Select|Wrap|Line Numbers
  1.         public override string ToString()
  2.         {
  3.             return string.Format("{0}: {1}", GetTypecharFromEnum(myType), myPath);
  4.         }
  5.  
My only comment is how you are formatting.
Expand|Select|Wrap|Line Numbers
  1. return String.Format(_carColour + " " + _carMake);
Really doesn't make use of the String.Format in any way.

You could just as easily
Expand|Select|Wrap|Line Numbers
  1. return _carColour + " " + _carMake;
or if you want to use String.Format then:
Expand|Select|Wrap|Line Numbers
  1. 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.
Dec 21 '09 #3
truezplaya
115 New Member
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
Dec 23 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: GMan | last post by:
Is there a way to override an event that is inherted from a base class so it won't be seen in the derived class? Also is there a way to override an override method that is inherted frmo a base...
2
by: Pankh | last post by:
Hi, Is it possible to restrict overiding a function in derived class? To be more clear, I have base class with function foo() and I do not want any derived class to implement foo() again. Always...
3
by: Viktor Marohnic | last post by:
Hello. I would to do something like this. container = p1 = point() l1 = line() and i would like to override = method of the module so that its puts all objects into container.
3
by: Vincent V | last post by:
Hey im Overiding OnInit in my Custom Page class What i want to be able to do is pass in Some Values Ie PageID PageCategory, PageSubCategory How can i pass in some Vairables So my Custom Page class...
101
by: Sean | last post by:
Book I am reading says that Cstr() is best method for efficency and safety however it doesnt compare that method of the .ToString() method. Which is best. Thanks
7
by: mirandacascade | last post by:
O/S: Windows XP Home Vsn of Python: 2.4 Copy/paste of interactive window is immediately below; the text/questions toward the bottom of this post will refer to the content of the copy/paste ...
12
by: Ron M. Newman | last post by:
hi, without going case-by-case, how do I know a "ToString" method returns useful information, like for Sytem.Int32, or useless information like for System.Drawing.Bitmap (returning the name of...
19
by: Michael C | last post by:
If we have something like this object x = null; MessageBox.Show(x.ToString()) we get an error. That kindof makes sense but there is no reason the behaviour couldn't be to return an empty...
3
by: vainstah | last post by:
Hello Guys and Galls, To start off, I have reached the solution I was looking for, but I would like comments and feedback on the solution I have reached and tips/tricks on making it more elegant....
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.