4.9.1 Displaying Variables

You can use the Watch window to monitor the value of a given variable throughout the execution of your application. For example, do, for, and while loops can cause problems when they don't terminate correctly. By displaying loop variables in the Watch window, you can determine whether a loop variable achieves its proper value.

To add a variable to the Watch window, perform the following steps:

1.In the Source window, use the mouse or the arrow keys to position the cursor on the name of the variable you want to watch.

2.From the Watch menu, choose Add Watch, or press CTRL+W.

An Add Watch dialog box appears with the selected variable's name displayed in the Expression field.

3.Choose the OK button or press ENTER to add the variable to the Watch window.

If you want to add a variable other than the one shown in the dialog box, type its name over the one displayed and press ENTER.

Adding a Watch variable opens the Watch window automatically if it is not already open. The Watch window appears at the top of the screen.

When you add a local variable, the following message may be displayed:

Watch Expression Not in Context

This message appears when execution has not yet reached the C-language function that defines the local variable. Global variables (those declared outside C-language functions) never cause CVW to display this message; you can watch them from anywhere in the application.

If any two or more applications or DLLs you are debugging contain global variables with the same name, CVW displays the variable of only the first application or DLL containing that variable name.

For example, if you are debugging App1 and App2, which both contain a global variable named hInst, CVW always displays the value of hInst in App1—even if CVW stopped at a breakpoint in App2.

The Watch window can display as many variables as you like; the quantity is limited only by available memory. You can scroll through information in the Watch window to view other variables. CVW automatically updates all watched variables as the application runs, including those not currently visible.

To remove a variable from the Watch window, do the following:

1.From the Watch menu, choose Delete Watch.

2.Scroll through information in the Delete Watch dialog box, and select the variable you want to remove.

Alternatively, you can position the cursor on any line in the Watch window and press CTRL+Y to delete the line.