ToString and the Visual Studio Debugger

Posted Thu, Apr 29 2010 15:50 by Deborah Kurata

It is always useful to override the ToString method on each your classes. This is especially true when using the Visual Studio debugger.

This example uses the list created in this prior post.

The list is returned from a function and the breakpoint is set immediately after  the return statement. Here is what the data tip looks like when hovering over the custList variable.

In C#:

image

In VB:

image

Notice how the items in the list show their index (0 through 3) in square braces and their class name in the curly braces. You can use the + to access the TreeView and see the property details for the class. But if you have lots of properties, you could be scrolling around to find the key bits of information you need.

You can change the information in the curly braces to contain more useful information by simply overriding the ToString method.

In C#:

public override string ToString()
{
    return this.CustomerId + " (" + this.LastName + ")";
}

In VB:

Public Overrides Function ToString() As String
    Return Me.CustomerId & " (" & Me.LastName + ")"
End Function

In this example, the most important pieces of information about the customer are the Id and last name. So that is what is returned from the ToString method. In your classes, you can return whatever information is useful for your debugging.

Now, when using the same debugging techniques described above, you can see the following when hovering over the custList variable:

In C#:

image

NOTE: For C#, this feature is available in VS 2008 and VS 2010.

In VB:

image

NOTE: For VB, this feature is new in VS 2010.

Now your most useful properties are displayed directly in the data tip. No need to navigate the property tree.

Override the ToString method on every business object to take advantage of this very useful debugging feature.

Enjoy!

Filed under: , , , ,

Comments

# re: ToString and the Visual Studio Debugger

Friday, April 30, 2010 8:07 AM by JohnS

Using ToString() is open to abuse by clients of your class.

Better to use the DebuggerDisplay attribute to achieve the same result.

See this post:

msdn.microsoft.com/.../ms228992.aspx

# re: ToString and the Visual Studio Debugger

Friday, April 30, 2010 10:17 AM by Deborah Kurata

Hi John -

Thanks for the tip. Can you give some more detail on the type of abuse you are referring to?

Thanks again!

# re: ToString and the Visual Studio Debugger

Thursday, May 06, 2010 9:05 AM by Rostov

I almost always put a 'ToString' on my custom objects anyway -- this way, diagnostic readouts or dumps out of lists can be done using a #DEBUG condition for containing extra information, like IDs, when I am testing, and then when I deploy a release, I display something more pertinent and without sensitive data.

I'm also curious as to what abuse John is talking about: given the 'precious time' of a programmer, I'd rather create something that CAN be used more often, such as placing said objects into listboxes, comboboxes, etc -- and have "short display" pieces of information retrieved that's consistent throughout the application.

# re: ToString and the Visual Studio Debugger

Sunday, September 18, 2011 7:12 PM by Reno

Hats off to whoever wrote this up and peostd it.

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: