Geeks With Blogs

News



Microsoft Store

Support This Site


AddThis Social Bookmark Button

Locations of visitors to this page

Subscribers to this feed

TwitterCounter for @sdorman

Creative Commons License


Scott Dorman ephemeral segment

The Watch window (actually there are four different Watch windows you can use) is easily the most powerful aspect of the Visual Studio debugger and the underlying technology it uses actually surfaces in several places:

  • Watch
  • Autos
  • Locals
  • Quick Watch
  • “Data Tips” (the debugging tool tips that appear when you hover over a code element)

The thing that most people might not realize is that you can actually call methods in the Watch window. This is useful because it allows you to see data structures that might not otherwise display well and you can also have completely dynamic assertions.

Calling a method in the Watch window is as simple as adding the parenthesis and any parameters needed.

image

There are a few considerations to be aware of when doing this, however. They are:

  • The function (property or method) executes in the context of the debuggee.
  • Don’t do anything more than reading memory.
  • The method must execute in less than 20 seconds.
  • Breakpoints are not hit when called from the Watch window.

Another very useful trick is that the Watch window supports drag and drop from the code editor. Simply select the expression in the code editor and drag it to the watch window.

Just like you can execute functions in the watch window, you can execute them in the Value column of the Watch window as well. This allows you to easily change the value of a watched expression to a completely new instance of an object. In the Value column, simply enter “new SomeClass()” to set the value to a new instance of the SomeClass object.

From the Immediate window you can type in an expression to allocate on object

   1: ? SomeClass x = new SomeClass()

and then in the Value column enter the variable prefixed with a “$” sign. (In this example, $x.)

Finally, let’s take a look at the Make Object ID capability of the debugger. This is available from the Locals and Autos windows through the context menu. Simply select the object you are interested in and right click, then choose the Make Object ID. This will create a numeric ID for that object, which will be displayed after the value.

What is actually happening here is that it tells the debugger to watch a particular object in memory no matter where it goes. This is useful because if the actual object goes out of scope you can still watch it by using the object ID. If the object ID says “Can’t evaluate” then it means the object has been garbage collected. Object IDs can also be used in conditional breakpoints and as parameters to function calls.

For instance, if you wanted to know what GC generation an object is in, you can do so using Object IDs.

Technorati Tags: , ,
DotNetKicks Image
Posted on Saturday, February 14, 2009 8:23 PM .NET (General) | Back to top


Comments on this post: Visual Studio 2008 Debugging – The Watch Window

# re: Visual Studio 2008 Debugging – The Watch Window
Requesting Gravatar...
hi,

I am working on a samll program development using MS Visual Studio 2005. I ve written couple of lines of code, and during run time when i place a variable in the watch window, i get
"CXX0017: Error: symbol "integer" not found"
i am not sure what the problem is, but the program executes and works fine.but i will be into trouble when i try to debug the code on later part of development.

any suggestion would help.

naveen
Left by naveen on Feb 25, 2009 4:05 PM

# re: Visual Studio 2008 Debugging – The Watch Window
Requesting Gravatar...
hi,

answering my previous post, disabling the optimization options in the project and file properties solved the problem..

thanks
naveen
naveenkrm@gmail.com
Left by naveen on Feb 26, 2009 2:16 PM

# re: Visual Studio 2008 Debugging – The Watch Window
Requesting Gravatar...
hi! I closed window witch Watch ... where can I find it?

Thanks!
Left by biosys on Aug 08, 2009 9:26 AM

# re: Visual Studio 2008 Debugging – The Watch Window
Requesting Gravatar...
right click on the variable and you'll see the option
Left by seed on Sep 10, 2009 10:15 AM

# re: Visual Studio 2008 Debugging – The Watch Window
Requesting Gravatar...
i have the same problem
where click on variable ?
Left by controlsys on Oct 06, 2009 3:23 AM

# re: Visual Studio 2008 Debugging – The Watch Window
Requesting Gravatar...
I am trying to add a variable to the watch window while my C# program is running, but the watch window is disabled and after selecting the variable and trying to drag it, nothing happens. Instead, I end up dragging a circle with a slash as if the operation not allowed. I have a breakpoint in the function. What am I doing wrong?
Left by William Prest on Oct 21, 2009 4:05 PM

# re: Visual Studio 2008 Debugging – The Watch Window
Requesting Gravatar...
It's very interesting feature but I also hve problems with finding it.
Left by pcontrol on Nov 20, 2009 6:32 AM

# re: Visual Studio 2008 Debugging – The Watch Window
Requesting Gravatar...
Watch Window - how to switch it on?

You run your app to be in the Debug mode. Then you go to the Debug toolbar (maybe you need to switch it on before). Then you find a drop-down menu and choose Watch1 window from there. (Probably there are other methods, this seems complicated but this how I did it).
Left by wiiiktor on Dec 18, 2010 6:25 AM

# re: Visual Studio 2008 Debugging – The Watch Window
Requesting Gravatar...
How can i watch the hole value of pin_ptr<const wchar_t> variable , not the first letter only ?
Left by Egor Kotov on Feb 21, 2011 11:59 AM

# re: Visual Studio 2008 Debugging – The Watch Window
Requesting Gravatar...
How to get get add watch window?
Left by soumya on Jan 18, 2012 6:48 AM

Your comment:
 (will show your gravatar)
 


Copyright © Scott Dorman | Powered by: GeeksWithBlogs.net | Join free