Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
In the preceding section, you learned that the Quick Info feature allows you to examine a variable’s value. In this section, you’ll learn to use the Locals window to assign new values to variables while your program is running. You’ll also use the Watch window to examine the value of more complex expressions.
1. | Inserting breakpoints. Clear the existing breakpoints. Then, set a breakpoint at line 21 in the source code by clicking in the margin indicator bar to the left of line 21 (Fig. H.10). Set another breakpoint at line 24 by clicking in the margin indicator bar to the left of line 24. Fig. H.10. Setting breakpoints at lines 25 and 28. |
2. | Starting debugging. Select Debug > Start. Type 13 at the Enter withdrawal amount for account1: prompt and press Enter so that your program reads the value you just entered. The program executes until the breakpoint at line 21. |
3. | Suspending program execution. The debugger enters break mode at line 21 (Fig. H.11). At this point, line 18 has input the withdrawalAmount that you entered (13), lines 19–20 have output that the program will attempt to withdraw money and line 21 is the next statement that will execute. Fig. H.11. Program execution suspended when debugger reaches the breakpoint at line 25. |
4. | Examining data. In break mode, you can explore the values of your local variables using the debugger’s Locals window, which is normally displayed at the bottom left of the IDE when you are debugging. If it is not shown, you can view the Locals window, select Debug > Windows > Locals. Figure H.12 shows the values for main’s local variables account1 and withdrawalAmount (13). Fig. H.12. Examining variable withdrawalAmount.
|
5. | Evaluating arithmetic and boolean expressions. You can evaluate arithmetic and boolean expressions using the Watch window. You can display up to four Watch windows. Select Debug > Windows > Watch 1. In the first row of the Name column, type (withdrawalAmount + 3) * 5, then press Enter. The value of this expression (80 in this case) is displayed in the Value column (Fig. H.13). In the next row of the Name column, type withdrawalAmount == 3, then press Enter. This expression determines whether the value of withdrawalAmount is 3. Expressions containing the == operator (or any other relational or equality operator) are treated as bool expressions. The value of the expression in this case is false (Fig. H.13), because withdrawalAmount currently contains 13, not 3. Fig. H.13. Examining the values of expressions.
|
6. | Resuming execution. Select Debug > Continue to resume execution. Line 21 debits the account by the withdrawal amount, and the debugger reenters break mode at line 24. Select Debug > Windows > Locals or click the Locals tab at the bottom of Visual Studio to redisplay the Locals window. The updated balance value in account1 is now displayed in red (Fig. H.14) to indicate that it has been modified since the last breakpoint. Click the plus box to the left of account1 in the Name column of the Locals window. This allows you to view each of account1’s data member values individually—this is particularly useful for objects that have several data members. Fig. H.14. Displaying the value of local variables.
|
7. | Modifying values. Based on the value input by the user (13), the account balance output by the program should be $37. However, you can use the Locals window to change the values of variables during the program’s execution. This can be valuable for experimenting with different values and for locating logic errors. In the Locals window, expand the account1 node and double click the Value field in the balance row to select the value 37. Type 33, then press Enter. The debugger changes the value of balance and displays its new value in red (Fig. H.15). Fig. H.15. Modifying the value of a variable.
|
8. | Setting a breakpoint at at main’s closing brace. Set a breakpoint at line 25 in the source code to prevent the program from closing immediately after displaying its result. If you do not set this breakpoint, you won’t be able to view the program’s output before the console window closes. |
9. | Viewing the program result. Select Debug > Continue to continue program execution. Function main executes until the return statement in line 29 and displays the result. Notice that the result is $33 (Fig. H.16). This shows that Step 7 changed the value of balance from the calculated value (37) to 33. Fig. H.16. Output displayed after modifying the account1 variable. |
10. | Stopping the debugging session. Select Debug > Stop Debugging. This will close the Command Prompt window. Remove all remaining breakpoints. |