Results 1 to 3 of 3

Thread: why we need to override tostring method

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Location
    fgh
    Posts
    332

    why we need to override tostring method

    hi,

    i have found a class with two properties, name and id...

    id is set and get directly using property ....

    but name is only set using property and returning name using overriding the tostring method in the class....

    i have seen many times, that the tostring() method is overrided in a class... can anyone brief why that done ?

    thankzzzzzzz
    gh

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: why we need to override tostring method

    You override it so that when the method is called with an instance of your class then you can control what is returned by the ToString method. You can combine different fields like first name and lastname,etc...
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: why we need to override tostring method

    Overriding the ToString method can be invaluable for custom classes. For example, an Employee class could override its ToString method to return a formatted string containing the first and last name.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width