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.

sets aside a portion of memory for the numeric array score( ) <strong>and</strong> places the default value 0<br />

in each element.<br />

The statements<br />

score(1) = 87<br />

score(3) = 92<br />

assign values to the first <strong>and</strong> third elements.<br />

The statements<br />

For i = 1 To 4<br />

picBox.Print score(i);<br />

Next i<br />

then produce the output 87 0 92 in picBox.<br />

EXAMPLE 1<br />

The following program creates a string array consisting of the names of the first five World Series winners.<br />

Figure 6-2 shows the array created by the program.<br />

’Create array for five strings<br />

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

Private Sub cmdWhoWon_Click()<br />

Dim n As Integer<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” ‘<br />

Access array of five strings<br />

n = Val(txtNumber.Text)<br />

picWinner.Cls<br />

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

End Sub<br />

[Run, type 2 into the text box, <strong>and</strong> click the comm<strong>and</strong> button.]<br />

Creating <strong>and</strong> Accessing Arrays 163

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

Saved successfully!

Ooh no, something went wrong!