Debugger Tips, Tricks and Tools #6
Create an Object ID to keep track of an object while debugging
In yesterday's tip I hinted at another new feature of the debugger specially designed for C# and J# programmers. This is the ability to create an Object ID for any particular object during your debugging session, no matter what your current context is. In other words, it's quite similar to being able to get the address of an object in C or C++ so that you can refer to that particular object at any time during debugging.
For example, given the following short program:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<string> stringList = new List<string>();
stringList.Add("Hello");
stringList.Add("World");
foreach (string str in stringList)
{
Console.WriteLine(ReverseString(str));
}
}
static string ReverseString(string str)
{
if (str.Length > 1)
{
return ReverseString(str.Substring(1)) + str.Substring(0, 1);
}
return str;
}
}
}
Put a breakpoint on the first line of ReverseString and run the program under the debugger. When the breakpoint is hit, hover your mouse over the 'str' variable, and on the DataTip that appears right click to bring up the context menu.
You will see the following:
Note the highlighted menu item called "Make Object ID". Select that and your DataTip will change to show the value of 'str' to be "Hello" {1#}". What's that "1#" doing on the end of the value? That's the Object ID. You can use that expression to refer to that particular instance of the object anywhere you can evaluate expressions in the debugger.
To prove it, first press F5 again. You will break into ReverseString again, but this time the value of 'str' is "ello". Now add "1#" to the watch window. You'll see:
When you're done with that object you can right click on it in the watch window and choose "Delete Object ID".
Comments
Anonymous
October 04, 2006
Here are some great resources from our Debugging .NET geekSpeak with John Robbins. John's blog http://www.wintellect.com/Weblogs/CategoryView,category,John%20Robbins.aspxAnonymous
January 11, 2007
PingBack from http://dotnetdebug.net/2005/12/01/new-interesting-blog-on-vs-2005-debugger-tips/Anonymous
August 10, 2007
I am simply amazed at the JavaScript debugging with Visual Studio 2008. The really cool part is thatAnonymous
November 26, 2007
PingBack from http://feeds.maxblog.eu/item_433987.htmlAnonymous
August 26, 2008
PingBack from http://mdavey.wordpress.com/2008/08/26/flexunit-silverlight-and-other-random-thoughts/