19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

460 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

SIX WALKTHROUGHS<br />

The following walkthroughs use the debugging tools with the programming structures covered<br />

in Sections 2, 3, 4, <strong>and</strong> 5.<br />

STEPPING THROUGH AN ELEMENTARY PROGRAM: SECTION 2<br />

The following walkthrough demonstrates several capabilities of the debugger.<br />

1. Create a form with a comm<strong>and</strong> button (cmdButton) <strong>and</strong> a picture box (picBox).<br />

Set the AutoRedraw property of the picture box to True. (During the debugging<br />

process, the entire form will be covered. The True setting for AutoRedraw prevents<br />

the contents of the picture box from being erased.)<br />

2. Double-click on the comm<strong>and</strong> button <strong>and</strong> enter the following event procedure:<br />

Private Sub cmdButton_Click()<br />

Dim num As Single<br />

picBox.Cls<br />

num = Val(InputBox(“Enter a number:”))<br />

num = num + 1<br />

num = num + 2<br />

picBox.Print num<br />

End Sub<br />

3. Press F8, click the comm<strong>and</strong> button, <strong>and</strong> press F8 again. A yellow arrow points<br />

to the picBox.Cls statement <strong>and</strong> the statement is highlighted in yellow. This<br />

indicates that the picBox.Cls statement is the next statement to be executed.<br />

(Pressing F8 is referred to as stepping. You can also step to the next statement<br />

of a program with the Step Into option from the Debug menu.)<br />

4. Press F8. The picBox.Cls statement is executed <strong>and</strong> the statement involving<br />

InputBox is designated as the next statement to be executed.<br />

5. Press F8 to execute the statement containing InputBox. Respond to the request<br />

by typing 5 <strong>and</strong> clicking the OK button.<br />

6. Press F8 again to execute the statement num = num + 1.<br />

7. Let the mouse sit over any occurrence of the word “num” for a second or so.<br />

The current value of the variable will be displayed in a small box. See Figure<br />

D.4.<br />

FIGURE D.4 Obtaining the Value of a Variable<br />

8. Click on the End icon to end the program.<br />

9. Move the cursor to the line<br />

num = num + 2<br />

<strong>and</strong> then press F9. A red dot appears to the left of the line <strong>and</strong> the line is displayed<br />

in white text on a red background. This indicates that the line is a break-

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!