90 likes | 212 Views
Arrays. Variable Arrays. a group of variables that have the same name and data type each element in a variable array is identified by a subscript refer to an array element by the name of the array followed by its subscript can have as many as 60 dimensions. Declaring One-dimensional Arrays.
E N D
Variable Arrays • a group of variables that have the same name and data type • each element in a variable array is identified by a subscript • refer to an array element by the name of the array followed by its subscript • can have as many as 60 dimensions
Declaring One-dimensional Arrays Syntax: Dim arrayname(lower subscript To upper subscript) As datatype Examples: Dim Friend(1 to 5) As String Dim Sums(20) As Double
Private Sub cmdWhoWon_Click() 'Create array for five strings Dim teamName(1 to 5) As String Dim n As Integer 'Fill array with World Series Winners teamName(1) = "Red Sox" teamName(2) = "Giants" teamName(3) = "White Sox" teamName(4) = "Cubs" teamName(5) = "Cubs" 'Access array of five strings n = Val(txtNumber.Text) picWinner.Cls picWinner.Print "The "; teamName(n); " won World Series number"; n End Sub
Private Sub cmdShow_Click() Dim total As Integer, student As Integer, average As Single Dim nom(1 To 8) As String, score(1 To 8) As Integer Open App.Path & "\SCORES.TXT" For Input As #1 For student = 1 To 8 Input #1, nom(student), score(student) Next student Close #1 total = 0 For student = 1 To 8 total = total + score(student) Next student average = total / 8 picTopStudents.Cls For student = 1 To 8 If score(student) > average Then picTopStudents.Print nom(student) End If Next student End Sub
Static and Dynamic Arrays • Static arrays have the dimensions specified in the array declaration. They cannot be redimensioned. • A dynamic array can have it's size changed after it has been declared. Dynamic arrays are declared without dimensions specified in the parenthesis. Dim arrayname( ) As datatype
Redimensioning Arrays • Use ReDim to change the size of a dynamic array • If you use the ReDim command in conjunction with the word Preserve, you can change the size of the array without destroying the data contained inside it.
ReDim Example Dim arrNums( ) As Integer Dim intArraySize As Integer intArraySize = 10 ReDim arrNums(intArraySize) For i = 0 to 9 arrNums(i) = i Next ReDim Preserve arrNums(20) For i = 10 to 19 arrNums(i) = i Next
Private Sub cmdShow_Click() Dim numStudents As Integer, nTemp As String, sTemp As Integer Dim student As Integer, total As Integer, average As Single Dim nom() As String, score() As Integer numStudents = 0 Open App.Path & "\SCORES.TXT" For Input As #1 Do While Not EOF(1) Input #1, nTemp, sTemp numStudents = numStudents + 1 Loop Close #1 ReDim nom(1 To numStudents) As String, score(1 To numStudents) As Integer Open App.Path & "\SCORES.TXT" For Input As #1 For student = 1 To numStudents Input #1, nom(student), score(student) Next student Close #1 total = 0 For student = 1 To numStudents total = total + score(student) Next student average = total / numStudents picTopStudents.Print average End Sub