Setting Your Watch

When you’re debugging a program with many local variables, you usually don’t want to watch all of them; you need to keep track of only a few. You can track specific variables and objects in the Watch window. You can have up to four Watch windows at a time in Visual Studio, but just the one in C# Express. Watch windows are like by-invitation versions of the Locals window; they list the objects you ask the debugger to keep an eye on, and you can see their values change as you step through the program, as illustrated in Figure 9-12.

A Watch window lets you keep an eye on just the variables you want to track.

Figure 9-12. A Watch window lets you keep an eye on just the variables you want to track.

The Watch windows are usually tabbed with the Locals window. You can add a watch by right-clicking on a variable and choosing Add Watch or you can just drag the variable to the Watch window. The variable will be added to your Watch window. To remove a variable that you’ve added to your Watch window, you can right-click on it in the Watch list and select Delete Watch.

In Visual Studio only, if you just need to peek at a variable, and perhaps to experiment with manipulating its value, you can right-click on it and choose QuickWatch, which opens a dialog box with watch information about a single object, as shown in Figure 9-13.

You can enter any expression into the Expression field and evaluate it from within the QuickWatch window. For example, suppose you had a QuickWatch on the length variable, which is set to 4.

The QuickWatch window lets you examine the properties of a specific variable.

Figure 9-13. The QuickWatch window lets you examine the properties of a specific variable.

If you want to know the impact of multiplying length by width (set to 8), enter:

length * width

into the Expression window and click Reevaluate. The value is shown in the Value window, as in Figure 9-14.

If you decide that you want to add the variable to a Watch window after all, click the Add Watch button.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset