C.3. The Locals and Watch Windows
In the preceding section, you learned that the Quick Info feature allows you to examine the value of a variable. In this section, you will learn how to use the Locals window to assign new values to variables while your program is running. You will also use the Watch window to examine the values of expressions.
- Inserting breakpoints. Set a breakpoint at line 23 (Fig. C.9) in the source code by left clicking in the margin indicator bar to the left of line 23. Use the same technique to set breakpoints at lines 26 and 28 as well.
Figure C.9. Setting breakpoints at lines 23 and 26.
- Starting debugging. Select Debug > Start Debugging. Type 49.99 at the Enter deposit amount for account1: prompt (Fig. C.10) and press Enter so that the program reads the value you just entered. The program executes until the breakpoint at line 23.
Figure C.10. Entering the deposit amount before the breakpoint is reached.
- Suspending program execution. When the program reaches line 23, Visual Studio suspends program execution and switches the program into break mode (Fig. C.11). At this point, the statement in line 20 (Fig. C.2) has input the depositAmount that you entered (49.99), the statement in lines 2122 has output that the program is adding that amount to the account1 balance and the statement in line 23 is the next statement that will execute.
Figure C.11. Program execution pauses when debugger reaches the breakpoint at line 23.
(This item is displayed on page 1448 in the print version)
- Examining data. Once the program has entered break mode, you can explore the values of your local variables using the debugger's Locals window. To view the Locals window, select Debug > Windows > Locals. Click the plus box to the left of account1 in the Name column of the Locals window (Fig. C.12). This allows you to view each of account1's instance variable values individually, including the value for balance (50). Note that the Locals window displays properties of a class as data, which is why you see both the Balance property and the balance instance variable in the Locals window. In addition, the current value of local variable depositAmount (49.99) and the args parameter of Main are also displayed.
Figure C.12. Examining variable depositAmount.
(This item is displayed on page 1448 in the print version)
- Evaluating arithmetic and boolean expressions. You can evaluate arithmetic and boolean expressions using the Watch window. Select Debug > Windows > Watch to display the window (Fig. C.13). In the first row of the Name column (which should be blank initially), type (depositAmount + 10) * 5, then press Enter. The value 299.95 is displayed (Fig. C.13). In the next row of the Name column in the Watch window, type depositAmount == 200, then press Enter. This expression determines whether the value contained in depositAmount is 200. Expressions containing the == symbol are bool expressions. The value returned is false (Fig. C.13), because depositAmount does not currently contain the value 200.
Figure C.13. Examining the values of expressions.
(This item is displayed on page 1448 in the print version)
- Resuming execution. Select Debug > Continue to resume execution. Line 23 executes, crediting the account with the deposit amount, and the program enters break mode again at line 26. Select Debug > Windows > Locals. The updated balance instance variable and Balance property value are now displayed (Fig. C.14).
Figure C.14. Displaying the value of local variables.
(This item is displayed on page 1448 in the print version)
- Modifying values. Based on the value input by the user (49.99), the account balance output by the program should be $99.99. However, you can use the Locals window to change variable values during program execution. This can be valuable for experimenting with different values and for locating logic errors in programs. In the Locals window, click the Value field in the balance row to select the value 99.99. Type 66.99, then press Enter. The debugger changes the value of balance (and the Balance property as well), then displays its new value in red (Fig. C.15). Now select Debug > Continue to execute lines 2627. Notice that the new value of balance is displayed in the Command Prompt window.
Figure C.15. Modifying the value of a variable.
(This item is displayed on page 1449 in the print version)
- Stopping the debugging session. Select Debug > Stop Debugging. Delete all breakpoints.
C 4 Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands
|