Arrays

We use Arrays to store lists of similar items, like class names or class marks. The allows us to reference all the items with the same name and therefore simplify coding by using loops.

Visual basic uses the DIM command to set up an array.

DIM StrName(5) as String ‘ creates a Array with 6 elements of type string

I have copied the following from the VBEE help file.

Array Bounds. You can specify the lower and upper bound of each dimension. To do this, you include a boundslist inside the parentheses. For each dimension, the boundslist specifies the upper bound and optionally the lower bound. The lower bound is always zero, whether you specify it or not. Each index can vary from zero through its upper bound value.

If you want to populate the array with details for testing this can be done as follows.

Array Initialization. You can initialize the values of an array by surrounding the initialization values with braces ({}).

Dim longArray(4) As Long = {0, 1, 2, 3}

However, you can use the arrays in different ways, the following bits of code both do the same thing. The code below creates an array of 6 locations (0-5), the first block stores the items in location 1-5, while the second in location 0-4

Dim StrName(5) As String
Dim Pupil As Integer
‘get names
For Pupil = 1 To 5
StrName(Pupil) = InputBox(“What is the name of pupil ” & Pupil)
Next
Dim StrName(5) As String
Dim Pupil As Integer
‘get names
For Pupil = 0 To 4
StrName(Pupil) = InputBox(“What is the name of pupil ” & Pupil)
Next