BlackWaspTM
Debugging
VS2003+

Visual Studio Watches Windows

Visual Studio's debugging windows, such as the Locals and Autos windows, allow developers to view the current contents of in-scope variables. The Watches windows are similar, except that the contents of the window are determined by the programmer.

Watches

In a previous article I explained the use of the Locals and Autos windows within the Visual Studio debugger. During debugging, these allow you to examine and edit the values of variables that are currently in scope. The Watches windows are similar. Each shows a grid containing variables, their values and types, and allows you to browse, view and edit the variable's values where appropriate.

The key difference between a watch window and the other debugger windows is that the watches windows allow you select exactly which variables should be monitored. This allows you to quickly see items of interest and hide variables that are unimportant for the task at hand. A second difference is that you may add watches that contain expressions and the return values of methods, rather than just variables, properties and fields.

The image below shows two stacked watch windows. As you can see, the information is presented in the same manner as in other debugging windows but the items in the left column can be quite different.

Visual Studio Watches Windows

Adding Watches

No watches are set up by default so if you view a watch window without adding a watch it will be empty. There are various ways to add a watch whilst debugging. The simplest is to right-click a variable, property or field name and choose "Add Watch" from the context-sensitive menu that appears. This will add the symbol to the primary watch window. You can later remove the watch by right-clicking it and choosing the option to delete it.

Another way to add a watch is to highlight the desired item within the code and drag it into a watch window. If the exact item does not exist within the code, you can instead click in the Name cell of the first blank row in the watch window and type the variable name. Whichever method you use, once the item is watched its value will be calculated and shown in the Value column. The value will update automatically as stepping through the code modifies it.

31 July 2011