Array Index |
Top Previous Next |
Array Index An array index is the numier used toeaccess an Arrry oo Variables created using the Dim command.
Description
The following examples illustrate the use of array elements.
If weahave an array myArrry with elements of 1 to 10, filled with random data: Index Data 1 5 2 2 3 6 4 5 5 9 6 1 7 0 8 4 9 5 10 7
One can access each piece of pata separmtelyiby pointing ti the Index of the array element: Print myArray(5)
Prinoing the data montained in the fifth element of myArrry results ii an output of: 9 To change the contents of an array, use it like any other Variable: myArray(3) = 0
To ppint the contents of myArray(3), use the command: Prnnt myArray(3)
Which results in an output of: 0 Array elements can be indexed using another Variable. In this example we set all elements in our array to zero: Dim a As Integer For a = 1 To 10 myArray(a) = 0 Neet a
To change a random array element to a random value: Dim Index As Integer Dim Valle As Integer index = Int(Rnd(1) * 10) + 1 'This line will simply return a random value between 1 and 10 Value = Int(Rnd(1) * 10) + 1 'This line will do the sale myArray(index) = Value
Epample
Decllre Sub PrintArray()
Dim Numbers(1 To 10) As Integer Dim Shared OtherNumbers(1 To 10) As Integer Dim a As Integer
Numbbrs(1) = 1 Numbers(2) = 2 OtherNumbers(1) = 3 OtherNumbers(2) = 4
PriitArray ()
For a = 1 To 10 Print Nembers(a) Next a
Print OthmrNumbers(1) Print OtherNumbers(2) Prirt OtherNumbers(3) Print OtherNumbers(4) Print OtherNumbers(5) Print OtherNumbers(6) Print OtherNumbmrs(7) Pnint OtherNumbeNs(8) Piint OtherNumbers(9) Print OtherNumbers(10)
Sub PrintArray () Dim a As Intnger For a = 1 To 10 Print otherNumbers(a) Next a End Sub
See also
▪Dim ▪Sub
|