Write simple Debug helpers to help you debug and maintain your code
Much of my time is spent using the Visual Studio debugger examining code to figure out how it works and how to fix it. When stepping through a function, the values the function uses are very useful for code understanding. The debugger shows these values in the Watch/Locals/Auto/Callstack windows. For example, the Locals window shows the Local variables used by the current code while at a breakpoint. Each variable might be a structure or class with many members. You can click to expand to examine sub-members of a value.
I also like to modify code while the debug session is live, so I can see real values while writing. Edit and Continue is a very useful feature, but it doesn’t work in every case, such as creating new methods or types. Also, native code doesn’t support EnC. So I like using an external editor (perhaps another instance of VS) to modify the code. This means that your debug session is still live, and the source code file still matches the binary, until you save the file in the external editor.
The string shown for a variable can be customized to help understand what the code is doing. As you step through the code, if you can understand at a glance what the function is doing, rather than drilling into sub-members, you’re more efficient. Some types have dozens of variables, and some might be more important than others.
For native code (C++, C) you can customize the display string with this: Customize the VS debugger display of your data
For managed code (VB, C#) you can override the ToString method to customize the string. In VS 2008, the debugger will display the results of calling the ToString method.
There are 2 options:
Enable Property Evaluation and other implicit function calls
The next version of Visual Studio will add VB to this feature!
As a simple workaround, to have the debugger call ToString in VB programs, you need only prefix your types with
The line continuation character won’t be needed in the next version either!
If you don’t have the source code or can’t modify it, you can still modify the displayed string: Customize the display of types in the Debugger using Extension Methods and DebuggerDisplay Attribute
Below are a VB and C# program demonstrating how the string can be customized.
Start VS2008, choose File->New->Project->C# or VB->WPF Application, then paste in the code.
Put a Breakpoint on the indicated line and hit F5. You’ll see the string displayed is the result of the ToString function.
Sub dd() Handles Me.Loaded
Dim y = New TestClass
Dim r = "Put a bpt here"
Dim x = "asdf"
Public Overrides Function ToString() As String
Return "here we are in tostring " + x
Here’s the C# equivalent Program
/// Interaction logic for Window1.xaml
public partial class Window1 : Window
var x = new TestClasss();
var y = "set a bpt here";
String foo = "test";
public override string ToString()
return "i'm in the test class " + foo;