I'd like to get ToString() to display for a class under my control in debug mode.
It'd be nice if this was the first thing to show up when you hover over a variable with the mouse. Is there an attribute for this?
I'd like to get ToString() to display for a class under my control in debug mode.
It'd be nice if this was the first thing to show up when you hover over a variable with the mouse. Is there an attribute for this?
Mark your class with
[System.Diagnostics.DebuggerDisplay("{ToString()}")]
Test:
[System.Diagnostics.DebuggerDisplay("{ToString()}")]
class MyClass
{
private string _foo = "This is the text that will be displayed at debugging"
public override string ToString()
{
return _foo;
}
}
Now when you hover over a variable with the mouse it will show This is the text that will be displayed at debugging
.
There is DebuggerDisplayAttribute
which lets you influence the display. It allows you to write fairly complex expressions to produce the debug output, although it is not recommended to do so.
However, if you have overriden ToString
then the debugger is documented to display that by default. Maybe there's something wrong with the code?
What you are looking for is the DebuggerDisplayAttribute
:
http://www.codeproject.com/Articles/117477/Using-DebuggerDisplayAttribute
Use the above link to see how it's done and then apply this to your class, using the ToString()
method to drive what is shown. I've only ever used properties, not sure if you can inject classes.
The output of ToString
should be the default you see when debugging.
It can be overridden using the DebuggerDisplay
Attribute (see MSDN).
I prefer overriding the ToString
method because its easier and more versatile because it helps when writing to log files as well.
What output do you see? If you get the type name you see the default ToString
.
in the object Override the .ToString as follows:
public class MyObject
{
public int Property1{ get; set; }
public string Property2{ get; set; }
public string Property3 { get; set; }
public override string ToString()
{
return Property3;
}
}
This will return Property3 as the ToString() value
I had a similar issue. My class had an ToString() override and it still didn't show up in VS, which was odd.
Adding the attribute [System.Diagnostics.DebuggerDisplay("{ToString()}")] to the class showed an exception in the visual studio debugger, where the ToString should have displayed. Turned out I had a bug with incorrectly using string.Format() in my implementation. This is an interesting behavior - VS reverts to the default ToString in case of an exception. The usage of the mentioned attribute forces the display to show the method's output - valid or exception. This is very useful for debugging ToString(). Otherwise there is no point in adding this attribute explicitly to each class since classes have it turned on by default, unless one wants to turn this behavior off for some reason.
If you are using visual studio you could add a watch @ runtime om the yourvariable.ToString() line, this will show up in the bottom of your screen when it hits a breakpoint