Section C.3. The Locals and Watch Windows


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.

1.

Inserting breakpoints. Set a breakpoint at line 16 (Fig. C.9) in the source code by left clicking in the margin indicator bar to the left of line 16. Use the same technique to set breakpoints at lines 19 and 20 as well.

Figure C.9. Setting breakpoints at lines 16, 19 and 20.


2.

Starting debugging. Select Debug > Start Debugging. Type 25 at the Enter withdrawal 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 16.



Figure C.10. Entering the deposit amount before the breakpoint is reached.


3.

Suspending program execution. When the program reaches line 16, Visual Studio suspends program execution and switches the program into break mode (Fig. C.11). At this point, the statement in line 12 (Fig. C.2) has input the withdrawalAmount that you entered (25), the statement in lines 14-15 has output that the program is subtracting that amount from the account1 balance and the statement in line 16 is the next statement that will execute.

Figure C.11. Program execution pauses when debugger reaches the breakpoint at line 16.


4.

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 balanceValue (50). Note that the Locals window displays properties of a class as data, which is why you see both the Balance property and the balanceValue instance variable in the Locals window. In addition, the current value of local variable withdrawalAmount (25) is also displayed.

Figure C.12. Examining variable depositAmount


5.

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 (withdrawalAmount + 10) * 5, then press Enter. The value 175 is displayed (Fig. C.13). In the next row of the Name column in the Watch window, type withdrawalAmount = 200, then press Enter. This expression determines whether the value contained in withdrawalAmount is 200. Expressions containing the = symbol are Boolean expressions. The value returned is False (Fig. C.13), because withdrawalAmount does not currently contain the value 200.

Figure C.13. Examining the values of expressions.


6.

Resuming execution. Select Debug > Continue to resume execution. Line 16 executes, subtracting the account with the withdrawal amount, and the program enters break mode again at line 19. Select Debug > Windows > Locals. The updated balanceValue instance variable and Balance property value are now displayed (Fig. C.14).

Figure C.14. Displaying the value of local variables.


7.

Modifying values. Based on the value input by the user (25), the account balance output by the program should be 25. 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 balanceValue row to select the value 25. Type 37, then press Enter. The debugger changes the value of balanceValue (and the Balance property as well), then displays its new value in red (Fig. C.15). Now select Debug > Continue to execute lines 19-20. Notice that the new value of balanceValue is displayed in the Command Prompt window.

Figure C.15. Modifying the value of a variable.


8.

Stopping the debugging session. Select Debug > Stop Debugging. Delete all breakpoints.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net