Hi,

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

class Employee : IComparable<Employee>
{
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

Re: why we override ToString() 80 80

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.

Re: why we override ToString() 80 80

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();
        }
    }
}
Re: why we override ToString() 80 80

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());

Re: why we override ToString() 80 80

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

commented: Try to be sure about what you are saying. -3
Re: why we override ToString() 80 80

System.Object ToString() method defined as abstract method

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

commented: learn then comment it +0
Re: why we override ToString() 80 80

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

commented: Poor post -2
commented: Ahhh, programming doesn't teach you respect....waste +0
Re: why we override ToString() 80 80

@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

Re: why we override ToString() 80 80

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.

commented: Well said! +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.