Wednesday, August 29, 2007 11:50 AM
Here's a quick trick to help you out in debugging which a lot of people forget.
When you're viewing one of your own objects in the Visual Studio, either through one of the watch windows or simply by hovering over the object in the editor, you'll often simply see the type name of the object which doesn't really tell you that much.
What's happening? Well, under the covers the debugger is actually calling the
ToString() method on your object which, unless you've overrode it, only displays the qualified type name of your object.
Imagine that you have a collection of Customer objects that you are trying to sort, only every time that you examine the list of Customers you can't discern anything meaningful because all of the objects look alike...
Not much help is it? But wait, if the debugger simply calls the ToString() method on every object then we can simply override the method to return us something useful...like the name. By simply overriding the method to do exactly that...
/// <summary>
/// Returns a <see cref="T:System.String"/> that represents the current <see
/// cref="T:System.Object"/>. In this case, the current value of the <see cref="Name"/>
/// property is returned.
/// </summary>
/// <returns>A <see cref="T:System.String"/> that represents the current <see
/// cref="T:System.Object"/>.</returns>
public override string ToString()
{
return Name;
}
We get a readout that we can easily make much more sense out of...
Now all of those shady looking objects in your debugger can finally start to come into focus.