Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

(untagged)

10 Tips you should know about “Watch Window” While debugging in Visual Studio

, 25 Oct 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Watch windows is one of most commonly used debugging tool with Visual Studio. We generally used to explore the objects, values, properties and other nested objects as a tree structure. Most of the time we used watch window to only view the values or change the current object properties values to see

Watch windows is one of most commonly used debugging tool with Visual Studio. We generally used to explore the objects, values, properties and other nested objects as a tree structure. Most of the time we used watch window to only view the values or change the current object properties values to see the effects of changed object during debugging. But we can use watch windows for many different  purposes. In this blog post I am going to show 10 Tips, that may help you while dealing with Watch Window. 

Tips 1 : Calling Methods From Watch Window

As I said earlier, most of time we used watch window to explore the objects and it’s properties but  we can call a methods from watch window as well. If you are exploring some objects inside watch window, you can simple call any of the methods for that object inside watch window.

MethodCall1

As you can see from the above image, when you have typed stud object, you are able to access all the properties and methods that is accessible by the original object.

As for example, you can  pass the proper marks inside AddMarks() methods to get the result details as shown in below image.

MethodCall2

Here is another Tricks, you can use some “temp” variable to store those data. But you have to declare the variable at “Immediate Window”. [ Please read Tips 3 to know more about using Immediate window with Watch Window ]

So, just declare a variable in “immediate window” and used it inside watch window as shown in below snaps

MethodCall3

As addition you can used the same variable to do some additional operation  also.

MethodCall4

While calling some methods from watch window, please make sure the execution time for the methods should be less enough ( approx. less than 10 sec ). Otherwise you will get exception on evaluation time out.

Tips 2 : Drag-Drop & Copy-Paste Code inside Watch Window

While debugging we generally right click on the code block and select “Add to Watch” to explore the result. But you can drag and drop code expression from VS Editor to watch window. Or you can Copy and Paste a code expression to see the result. Below snaps showing the uses of the same.

dragdrop1

Similarly you can drag and drop object values inside a watch window too.

dragdrop2

Similar, Drag and Drop, you can copy the code expression from Visual studio editor and paste it inside watch window or with in Watch window.

Copypaste1

When you copy the source code and paste it, watch window will automatic show you the result. You can do the copy paste with in watch window as well.

Copypaste3 

This has many advantages while you are dealing with large object and working with multiple watch window ( See Tips 4) .

Tips 3: Use Runtime Object’s With Help of Immediate  Window

This is very interesting tips.  You always debug with the context of current object, but what if, If you need change the object instead of object value.  You can instantiate a new object by just type new ClassName(), which will generate you the new instance of your class as shown in below picture.

newobj1

But, you need to take help of Immediate window if you want to declare and object to used it further

newobj2

As shown in above image you can create new object and used it immediate window.

Now you may think, why I used Immediate window to declare the object, because Immediate window does not allow you declaration statements.

newobj3

 

Tips 4 : Using Multiple Watch Window

Ok, Till now what I have discussed is all about a single watch window. But Visual Studio provides us 4 different watch window. They are Watch 1, Watch 2, Watch 3, Watch 4

multiple1

This watch windows are really help when you are dealing with large object or want to see some data based on different classification. May be you can use different watch window for different objects as well. This will make the debugging very simple.

multiple2

As per the above image you can see we can use multiple watch window to watch different object. This use is very handy when you are working with a bit complex application where you have many object to view same time.

Tips 5 : Moving Values between multiple Watch Window

This is continuation of Tips 4, as I have discussed about working with multiple watch window, you can also pass values among them either by Copy Paste or Drag and Drop. Yes, this  looks very simple but it is very helpful !

moving1 

As the above image showing we can drag and drop items between watch windows similarly we can do copy paste values among watch windows

moving2

you have a large set of properties in a single objects, which is very difficult to watch in a single window and even you don’t want to check them all. Now you can move only the required members to different watch window to make your life simple.

Tips 6 : Create Object ID

By using “Make Object ID” option we are informing Visual Studio Debugger to keep track of that object no matter it’s within scope or out of scope for the current context.  We can create “Object ID” either from Locals, Autos or from Watch Windows. Object ID is a integer number followed by a pound (#) sign. When we create Object ID for an particular object, Visual Studio Debugger ( CLR Debugging Services )  use an integer value to uniquely identify the object. This “Object ID” allows you to get the object details even if it is out of scope.

1

 

I have published one complete article on the same, please read it from

How to track an object which is Out of Scope while Debugging ?

Tips 7 : Different Type of Display Format

Watch window allows you to view the variables values in Hex Mode. This is very simple features, Just Right Click on Watch window and select Hexadecimal Display.

hex1

Once you select “Hexadecimal Display”,  you will get the screen similar like below

hex2

But, You have noticed that all values has changed to hexadecimal format, but what If, If you want to do it for particular values, not for all. Let’s say, you want to get hex value for variable “b”. Here is the trick, you can use some comma (,) separated format “d” for Integer and “h” hex with in watch window to get the list of values.

hex3

From above image you can see, when we have “Hexadecimal display” on, we can still see integer value using “d” with the variable name. Similarly we can get the hex value of any variables with out change the display mode of watch window

hex4

Tips 8 : Get Object Generation

If you are dealing with any objects in Watch window and you want to check the current generation for that Object you can easily get that by calling GC.getGeneration(objectName).

Gc1

Gc2

Note : This is also applicable for newly created object Id’s as described in Tips 6

Tips 9 : View Pseudo variable

You can view pseudo variable with in Watch window.

pseudo

Here is nice article in MSDN which described about  Debugging With Pseudo Variables And Format Specifiers

Tips 10: Use XML/HTML/Text Visualizer from Watch Window

Like normal visualize objects, variable in VS code editor, you can view  XML, HTML and Text Visualizer from watch window itself.

Viz1

Summary : In this post, I have described about 10 useful tips with related to watch window. Few of them we use frequently.

Hope this will helps you.

Thanks !

AJ


Filed under: Debugging, Tips and Tricks, Visual Studio

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Abhijit Jana
Technical Lead
India India
.NET Consultant | Former Microsoft MVP - ASP.NET | CodeProject MVP, Mentor, Insiders| Technology Evangelist | Author | Speaker | Geek | Blogger | Husband
 
Blog : http://abhijitjana.net
Web Site : http://dailydotnettips.com
Twitter : @AbhijitJana
My Kinect Book : Kinect for Windows SDK Programming Guide
Follow on   Twitter

Comments and Discussions

 
QuestionThank you Pinmemberrakithadharmasena1-Jun-14 18:23 
GeneralMy vote of 5 PinmentorKunalChowdhury26-Oct-10 0:46 
Generaluser login Pinmembercutegirl125-Oct-10 22:12 
AnswerRe: user login PinmemberAnkur\m/2-Jan-11 20:31 
GeneralMy vote of 5 PinmemberHiren Solanki25-Oct-10 21:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 26 Oct 2010
Article Copyright 2010 by Abhijit Jana
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid