954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

why we override ToString()

Hi,

I have seen in couple of places the code where ToString() method has been overriden. Like :

class Employee : IComparable
{
public int Salary { get; set; }
public string Name { get; set; }

public int CompareTo(Employee other)
{
if (this.Salary == other.Salary)
{
return this.Name.CompareTo(other.Name);
}
return other.Salary.CompareTo(this.Salary);
}

public override string ToString()
{
// String representation.
return this.Salary.ToString() + "," + this.Name;
}
}

What is the need of overriding. I have seen if we dont override we do not get the value instead we get the object only. Why so ?

Plz reply if you have any idea.

Thanks.
Regards

surajrai
Newbie Poster
16 posts since May 2010
Reputation Points: 10
Solved Threads: 1
 

toString is a method from the Object class. every class inherits at some point of the Object class.

If you do not over write the method in your new class, then it moves on to the next definition available... in the events that your class does not extend anything or that the class it extends does not overwrite toString either, then the original method from object will eventually be used.

Philippe.Lahaie
Posting Whiz
326 posts since Oct 2007
Reputation Points: 103
Solved Threads: 45
 

here is a little sample project to demonstrate this :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace toStringTests
{
    class Program
    {
        static void Main(string[] args)
        {
            Object o = new object();
            StringOverrider so = new StringOverrider("some string");
            ExtendingClass ec = new ExtendingClass(7, "some other string");

            Console.WriteLine("Object :\n" + o.ToString());
            Console.WriteLine("\nStringOverrider :\n" + so.ToString());
            Console.WriteLine("\nExtendingClass :\n" + ec.ToString());
            Console.ReadLine();
        }
    }

    class StringOverrider 
    {
        string myString;

        public StringOverrider(String s)
        {
            myString = s;
        }
        public override string ToString()
        {
            return "my overrided string (" + myString + "): " + base.ToString();
        }
    }

    class ExtendingClass : StringOverrider
    {
        int myInt;

        public ExtendingClass(int i , String s):base(s) 
        {
            myInt = i;
        }
        public override string ToString()
        {
            return "my second overrided string (" + myInt + "): " + base.ToString();
        }
    }
}
Attachments output.png 78.55KB
Philippe.Lahaie
Posting Whiz
326 posts since Oct 2007
Reputation Points: 103
Solved Threads: 45
 

I override it to also create the output I want from a class when I archive it.

I will then use the .ToString().GetHashCode() to override the GetHashCode() method
I will also override the .Equals() with ToString().Equals(((MyObj)obj).ToString());

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

Hi dude.,

Do you know System.Object is the root class of .net framework from this only all the class are derived. In System.Object ToString() method defined as abstract method because fields in object is not stable format so we must need to override ToString() method to covert object into string

mani-hellboy
Junior Poster in Training
69 posts since Feb 2012
Reputation Points: 0
Solved Threads: 7
 
System.Object ToString() method defined as abstract method


Incorrect information, read this: http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 
Incorrect information, read this: http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx


would you know programming

what is the use of Overridable and overrides

Plz learn it then comment it

Your given link also says that it`s pure abstraction syntax only

you are waste

mani-hellboy
Junior Poster in Training
69 posts since Feb 2012
Reputation Points: 0
Solved Threads: 7
 

@mani-hellboy: you must be a clever man, that you are able to read the word abstract or the word abstraction in the link I posted. Between the lines maybe?
Also suggest you to read the rules: http://www.daniweb.com/forums/faq.php?faq=daniweb_policies

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

One important use of overriding the ToString method in your classes is for consistency in the display of information in your views. It is far easier to override the ToString method than to remember the format used in each view for your class.

Say for example that I have a class called Employee. Let's say I want my employee record to always be displayed as first name followed by a space followed by the surname. Overriding the ToString method allows it to be performed in one place, while you may have several views containing employee information, say an employee list, a sales figures list and a graphical representation of their sales figures. To be consistent I would have to apply this formatting in three places rather than the one.

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: