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.

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

FIGURE 6-2 The Array TeamName( ) of Example 1<br />

In Example 1, the array teamName was assigned values within the cmdWhoWon_Click<br />

event procedure. Every time the comm<strong>and</strong> button is clicked, the values are reassigned to the<br />

array. This manner of assigning values to an array can be very inefficient, especially in programs<br />

with large arrays where the task of the program (in Example 1, looking up a fact) may<br />

be repeated numerous times for different user input. When, as in Example 1, the data to be<br />

placed in an array are known at the time the program first begins to run, a more efficient<br />

location for the statements that fill the array is in <strong>Visual</strong> <strong>Basic</strong>’s Form_Load event procedure.<br />

The Form_Load event procedure is executed by <strong>Visual</strong> <strong>Basic</strong> as soon as the program is run,<br />

<strong>and</strong> this execution is guaranteed to occur before the execution of any other event or general<br />

procedure in the program. Example 2 uses the Form_Load procedure to improve on<br />

Example 1.<br />

EXAMPLE 2<br />

Modify Example 1 to request the name of a baseball team as input <strong>and</strong> search the array to determine<br />

whether or not the team name appears in the array. Load the array values only once.<br />

’Create array for five strings<br />

Dim teamName(1 To 5) As String ‘in (Declarations) section of (General)<br />

Private Sub cmdDidTheyWin_Click()<br />

Dim team As String, foundFlag As Boolean, n As Integer<br />

’Search for an entry in a list of strings<br />

team = txtName.Text<br />

foundFlag = False<br />

n = 0<br />

Do<br />

n = n + 1<br />

If UCase(teamName(n)) = UCase(team) Then<br />

foundFlag = True<br />

End If<br />

Loop Until (foundFlag = True) Or (n = 5)<br />

‘Above line can be replaced with Loop Until (foundFlag) or (n = 5)<br />

picWinner.Cls<br />

If foundFlag = False Then ‘Can be replaced by If Not foundFlag Then<br />

picWinner.Print “The ”; team; “ did not win any”;<br />

picWinner.Print “ of the first five World Series.”<br />

Else<br />

picWinner.Print “The ”; teamName(n); “ won World Series number”; n<br />

End If<br />

End Sub<br />

Private Sub Form_Load()<br />

’Fill array with World Series winners<br />

teamName(1) = “Red Sox”<br />

teamName(2) = “Giants”<br />

teamName(3) = “White Sox”<br />

teamName(4) = “Cubs”<br />

teamName(5) = “Cubs”

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

Saved successfully!

Ooh no, something went wrong!