Stepping through a Program Containing a General Procedure: Chapter 4


[Page 632]

The following walkthrough uses the single-stepping feature of the debugger to trace the flow through a Sub procedure:

1.

Create a form with a button (btnPush) and a text box (txtBox). Then enter the following two procedures:

Private Sub btnPush_Click(...) Handles btnPush.Click   Dim p, b As Double   p = 1000         'Principal   GetBalance(p, b)   txtBox.Text = "The balance is " & FormatCurrency(b) End Sub Sub GetBalance(ByVal prin As Double, ByRef bal As Double)   'Calculate the balance at 5% interest rate   Dim interest As Double   interest = 0.05 * prin   bal = prin + interest End Sub


2.

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.

3.

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

4.

Press F8 once, and observe that the yellow arrow now points to the statement "p = 1000." This statement will be executed when F8 is next pressed.

5.

Press F8 again. The statement "p = 1000." was executed, and the yellow arrow now points to the statement calling the Sub procedure GetBalance.

6.

Press F8, and observe that the yellow arrow is now pointing to the header of the Sub procedure.

7.

Press F8 three times to execute the assignment statements. The yellow arrow now points to the End Sub statement. (Notice that the Dim and comment statements were skipped.)

8.

Press F8, and notice that the yellow arrow has moved back to the btnPush_Click event procedure and is pointing to the calling statement. By hovering the cursor over the variable b, you can verify that the Sub procedure was executed.

9.

Press Ctrl + Alt + Break to terminate debugging.

10.

Repeat Steps 2 through 5, and then press Shift + F8 to step over the Sub procedure GetBalance. The Sub procedure has been executed in its entirety.

11.

Press Ctrl + Alt + Break to terminate debugging.




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