15.04.2018 Views

programming-for-dummies

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

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

100<br />

Fixing a Program with a Debugger<br />

Step out<br />

Suppose you start examining a subprogram line by line and suddenly want<br />

to stop. As an alternative to stepping through the rest of the subprogram,<br />

you can use the step out command, which tells the computer, “Stop stepping<br />

through the subprogram line by line right now!”<br />

Watching variables<br />

When you step or trace through a program, line by line, you can see how the<br />

program works. For more insight into your program’s behavior, you can<br />

watch your variables.<br />

You can read more about variables in Book II, Chapter 2. For now, just think<br />

of a variable as a temporary place to store data, such as a number or a word.<br />

Watching a variable lets you see what data your program is storing and using<br />

at any given time. That way if your program is supposed to print a name but<br />

actually prints that person’s phone number, you can step through your program<br />

line by line, and watch to see which line stores the wrong data <strong>for</strong> the<br />

program to print.<br />

Not only can you “watch” how your program stores data, but a debugger<br />

also lets you change data while your program is running. By changing data,<br />

you can see how your program responds.<br />

Figure 4-7:<br />

The step<br />

over<br />

command<br />

lets you skip<br />

or “step<br />

over” the<br />

lines stored<br />

in a<br />

subprogram.<br />

Private Sub cmdOK_Click<br />

HandleReply()<br />

txtQuestion.Focus()<br />

End Sub<br />

With the Step Over command, the debugger<br />

treats the HandleReply() subprogram as a<br />

single line of code.<br />

Without the Step Over command, the debugger<br />

runs every line of code in the HandleReply()<br />

subprogram, <strong>for</strong>cing you to step through 23 lines<br />

of code.<br />

Public Sub HandleReply()<br />

Dim I As Object<br />

Dim NL As Object<br />

Const LOWER As Short = 0<br />

Const UPPER As Short = 9<br />

Static TalkArray(UPPER) As String<br />

Dim OldReply As String<br />

Dim TempString As String<br />

NL = Chr(10) & Chr(13)<br />

OldReply = lblReply.Text<br />

If lblConversation Text “” Then<br />

For I = LOWER To UPPER - 2<br />

TalkArray(I) = TalkArray(I+2)<br />

Next I<br />

End If<br />

TalkArray(UPPER - 1)= “ELIZA:” &lblReplay.Text<br />

TalkArray(UPPER) = txtQuestion.Text<br />

TempString = “”<br />

For I = LOWER to UPPER<br />

TempString = TempString & TalkArray(I) & NL<br />

Next I<br />

lblConversation.Text = TempString<br />

lblReply.Text = NewReply(OldReply, (txtQuestion.Text))<br />

txtQuestion.Text = “”<br />

End Sub

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

Saved successfully!

Ooh no, something went wrong!