This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.


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.


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.

Watching Expressions

Unlike the Locals and Autos windows, with a watch you can monitor the value of an expression. Expressions can contain calculations and are updated automatically when one of the underlying variables is changed. You can see an example expression in the above image. The watch with the name, "testVector.Length + testVector2.Length" sums the two Length properties.

You can watch an expression that already exists in the code by selecting the text and dragging it into a watch window. As with other watches, you can also type it directly into a watch window.

Calling Methods

Another useful type of watch is one that monitors the result of a method call. In the example image the watch, "testVector.Add(testVector2)" calls the Add method of the testVector variable, passing another Vector object to its parameter. The result of the method is displayed in the Value column. This type of watch is a variation upon watching an expression, so should either be dragged into the watch window or manually typed.

Side Effects

When you watch expressions you should be careful that there are no unintended consequences. For example, if you decide to watch to a predicate that compares two values, you may wish to watch the expression, "var1 == var2". If you accidentally use the assignment operator instead of the equality operator the value in var2 will be assigned to var1 every time the watch is evaluated, potentially changing the operation of the program being debugged.

You should also take care when calling methods from within watches. A method that performs a calculation and returns a value is generally safe to watch. If the method changes values or has other side effects you may inadvertently effect the program's execution in unexpected ways.

Using Multiple Watch Windows

The example image near the beginning of this article shows two watch windows stacked one above the other. You can actually use up to four watch windows. This allows you to organise your watches, perhaps using one window for each of several sections of code or to separate watches for different objects.

To view any of the watch windows, click the Debug menu at runtime. Choose the Windows submenu and then the Watch submenu. Four further menus, named "Watch 1" to "Watch 4" can be used to open each of the four available windows. To add items to each window, drag them from the code or type them into the first blank row of the grid. You can also drag and drop items between watch windows.

31 July 2011