Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
|
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 11th, 2008, 04:42 PM
Friend of Wrox
 
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default

It's starting to make more sense with these examples. I just still don't understand why you could not just make a new method to do the same thing then overriding the toString method?



 
Old November 11th, 2008, 04:49 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Perhaps the best reason for overriding ToString is the one that I casually noted: If you override ToString then many of the standard methods that *MUST* have a String argument can utilize any instance of your class to invoke ToString. If you use your own method name, these standard methods won't know it exists.

That is, you can do
    Whamboozle wham = new Whamboozle( );
    ...
    Console.WriteLine(wham);
whereas if you invented your own method for each class, you'd have to (a) remember what the method name was in each and every class and (b) explicitly invoke it:
    Console.WriteLine( wham.DebugDumpOfThisWhamboozleObject() );

Look at it this way: If it's good for 78.3794% of the classes in use today, it's probably okay for you to use it, too.
 
Old November 11th, 2008, 06:49 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Maybe this will help a little in addition to OP’s example.

First, the .NET common language runtime requires that every object be derived from Sytem.Object so that every object has a ToString() method (among others). The ToString() method can be used by an object to define its own value semantics, or the object can default to System.Object’s implementation of ToString(), which simply returns the object’s name.

Second, the reason all objects must implement a ToString() method is because many .NET framework components need to be able to call an objects ToString() method to function correctly. OP’s Console class example is an excellent one. Another is the whole Windows Forms Data Binding subsystem.

Maybe a visual will help. Code for a simple class named Person and a simple form with a ListBox is listed below.

When you initially run the program, the ListBox displays:

Person
Person

This is because Windows Forms Data Binding automatically calls the Person object's ToString() method when the ListBox binds to the object. Since the Person class doesn’t override ToString(), System.Object’s default implementation is used, and the object’s name is returned.

Now, override ToString() in the Person class by adding the following method:

Code:
public override string ToString()
{
        return String.Format("{0}, {1}", LastName, FirstName);
}
Now the ListBox displays:

Doe, John
Doe, Jane

In other words, Windows Forms Data Binding (and many other framework components) require your objects to implement a parameterless ToString() method in order to work correctly. This ‘contract’ between your objects and the Data Binding subsystem, for example, is enforced by requiring your object's to subclass System.Object.

Now, yes, you could write your own method to do the formatting ToString() overrides can do. But in doing so, you would deprive your objects of the ability to interact with framework components that require being able to make calls to a parameterless method named ToString().

Here’s the rest of the code.

Code:
using System;
public class Person
{
    public string FirstName;
    public string LastName;

    public Person() { }
}
Code:
using System;
using System.Windows.Forms;

public class Form1 : Form
{
    private BindingSource bindingSource = new BindingSource();
    private ListBox listBox1;

    static void Main()
    {
        Application.Run(new Form1());
    }

    public Form1()
    {
        // initialize Form.
        this.listBox1 = new System.Windows.Forms.ListBox();
        this.Controls.Add(this.listBox1);

        // bind listbox to binding source
        bindingSource.Add(new Person{ FirstName="John", LastName="Doe"});
        bindingSource.Add(new Person{ FirstName="Jane", LastName="Doe"});
        listBox1.DataSource = bindingSource;
    }
}
HTH,

Bob


 
Old November 11th, 2008, 09:09 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Excellent example! Much clearer than my Whamboozle class.

And I think I'll up the percentage to 88.2271%
 
Old November 11th, 2008, 10:19 PM
Registered User
 
Join Date: Jul 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How old are you? I can answer all the questions for you.but..
My english is not very well. if you need, you can ask me and I will answer your question.my email is [email protected]

I'm a programer in China.Studying English now. so I come here...
I have been studying program since 2003
 
Old November 11th, 2008, 10:20 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

LOL :) Ain't easy to top a Whamboozle class. Took everything I had!

 
Old November 11th, 2008, 11:01 PM
Friend of Wrox
 
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default

Thanks it makes a lot more sense.






Similar Threads
Thread Thread Starter Forum Replies Last Post
toString() praveena Java Basics 3 March 20th, 2006 10:52 PM
Request.UrlReferrer.ToString() mike72 BOOK: ASP.NET Website Programming Problem-Design-Solution 2 September 5th, 2005 09:31 AM
Problem with Request.UrlReferrer.ToString velateam .NET Framework 2.0 0 April 5th, 2005 08:22 AM
enum toString() overloading kramis8 C# 0 November 21st, 2004 07:55 PM
.ToString() functions cuts off zeros olambe BOOK: ASP.NET Website Programming Problem-Design-Solution 2 July 6th, 2004 10:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.