Stepping through a Program Containing a Do Loop: Chapter 6


[Page 635 (continued)]

Do Loops

The following walkthrough demonstrates use of the Watch window to monitor the value of a condition in a Do loop that searches for a name:

1.

Create a form with a button (btnPush) and a text box (txtBox). Then double-click on the button, and enter the following event procedure:

Private Sub btnPush_Click(...) Handles btnPush.Click   'Look for a specific name.   Dim searchName As String, name As String = ""   Dim sr As IO.StreamReader = IO.File.OpenText("DATA.TXT")   searchName = InputBox("Name:") 'Name to search for in list   Do While (name <> searchName) And (sr.Peek <> 1)     name = sr.ReadLine   Loop   sr.Close()   If name = searchName Then     txtBox.Text = name   Else     txtBox.Text = "Name not found"   End If End Sub


2.

Access Windows Notepad, enter the following lines of data, and save the file in the program's Debug subfolder inside the bin folder with the name DATA.TXT:

Bert
Ernie
Grover
Oscar


[Page 636]

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.

4.

Click on the button. The yellow arrow points to the header of the event procedure.

5.

Click on the variable searchName, click the right mouse button, and click on "Add Watch." The variable searchName has been added to a window titled Watch.

6.

Repeat Step 5 for the variable name.

7.

Press F8 four times to execute the File.OpenText statement and the statement containing InputBox. Enter the name "Ernie" in the input dialog box, and then click OK.

8.

Press F8 repeatedly until the entire event procedure has been executed. Pause after each keypress, and notice how the values of the expressions in the Watch window change.

9.

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