Stepping through an Elementary Program: Chapter 3


[Page 630 (continued)]

The following walkthrough demonstrates several capabilities of the debugger:

1.

Create a form with a button (btnPush) and a text box (txtBox).

2.

Double-click on the button and enter the following event procedure:

Private Sub btnPush_Click(...) Handles btnPush.Click   Dim num As Integer   num = CInt(InputBox("Enter a number:"))   num += 1   num += 2   txtBox.Text = CStr(num) End Sub


3.

Place the cursor on the line beginning Private Sub, press the right mouse button, and click on "Run to Cursor." The program will execute, and the form will appear.


[Page 631]

4.

Click on the button. In the Code window, a yellow arrow points to the Private Sub statement.

5.

Press F8. The yellow arrow now points to the statement containing InputBox to indicate that it is the next statement to be executed. (Pressing F8 is referred to as stepping into. You can also step to the next statement of a program with the Step Into option from the Debug menu or with the Step Into icon.)

6.

Press F8. The statement containing InputBox is executed, and an Input dialog box requesting a number appears. Respond to the request by typing 5 and clicking the OK button.

7.

Press F8 again to execute the statement num += 1.

8.

Let the mouse hover over any occurrence of the variable num for a second or so. The current value of the variable will be displayed in a small box.

9.

Press Ctrl + Alt + Break to stop debugging. (You also can stop debugging by clicking on "Stop Debugging" in the Debug menu.)

10.

Move the cursor to the line

num += 2


and then press F9. A red dot appears on the gray border to the left of the line. This indicates that the line is a breakpoint. Pressing F9 is referred to as toggling a breakpoint.

11.

Press F5 and click on the button. Respond to the request by entering 5 and pressing OK. The program executes the first three lines and stops at the breakpoint. The breakpoint line is not executed.

12.

Open the Immediate window by pressing Ctrl + Alt + I. If necessary, clear the contents of the window by pressing the right mouse button and selecting "Clear All." Type the statement

? num


into the Immediate window, and then press Enter to execute the statement. The appearance of "6" on the next line of the Immediate window confirms that the breakpoint line was not executed.

13.

Click on the Code window.

14.

Move the cursor to the line "num + = 1," click the right mouse button, and then click on "Set next Statement."

15.

Press F8 to execute the selected line.

16.

Return to the Immediate window by clicking on it. Type the statement "? num," and press Enter to confirm that the value of num is now 7. Then return to the Code window.

17.

Move the cursor to the breakpoint line, and press F9 to deselect the line as a breakpoint.

18.

Press F5 to execute the remaining lines of the program. Observe that the value displayed in the text box is 9.




An Introduction to Programming Using Visual Basic 2005
Introduction to Programming Using Visual Basic 2005, An (6th Edition)
ISBN: 0130306541
EAN: 2147483647
Year: 2006
Pages: 164

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