Visual Basic Arrays

In Visual Basic, an array is a special type of variable that contains multiple values of the same type. The following string variable, ‘fname’, has been defined and initialised with the name ‘Bob’.

Dim fname As String = "Bob"

If multiple names need to be stored, instead of having a different variable for each, an array could be used.

Dim fname(3) As String
fname(0) = "Bob"
fname(1) = "George"
fname(2) = "Fred"
fname(3) = "Alan"

The first line in the above example declares an array, which holds four string elements. Each element in an array has an index number, which is used in lines two to five above to populate the four elements. Notice that the first index number is a zero and not a one. The number three in the array declaration refers to the maximum index value allowed in the array and not the actual number of elements allowed in the array, hence why it can store four elements and not just three.

The above example can be shortened by declaring and initialising the array in one statement.

Dim fname = New String() {"Bob", "George", "Fred", "Alan"}

As well as being able to use the index number to populate a particular element in an array, it can also be used to extract the value of an element in an array, in order to carry out a particular task, or simply just to display it.

Console.WriteLine(fname(2))

Here, the element with an index number of two would be displayed, in this case “Fred”.

In order to display all of the elements in an array, a ‘For’ loop can be used.

For i = 0 To fname.Length - 1
    Console.WriteLine(fname(i))
Next

The above example uses the ‘length’ property of an array to determine the number of elements within it and therefore the number of times to go through the loop. The loop goes from zero to the length of the array minus one because of the fact that index numbers start at zero and not one. The resulting output will list all the elements of the array on a separate line.

Bob
George
Fred
Alan

Instead of explicitly stating zero as the first index value for the array and the length minus one as the last, the lower bound and upper bound values of the array can be used to produce the same output.

For i = fname.GetLowerBound(0) To fname.GetUpperBound(0)
    Console.WriteLine(fname(i))
Next

A variation on the ‘For’ loop that can be used to loop through an array is the ‘For Each’ loop, which simplifies the process. The below example produces the same results as the two above.

For Each name As String In fname
    Console.WriteLine(name)
Next

Although the size of an array needs to be specified when it is defined, it is possible to increase it later. This can be done in one of two ways, the first of which preserves any data that is already being stored within the array.

ReDim Preserve fname(7)

This will double the size of the ‘fname’ array, whilst preserving the elements that are already within it. If preserving its current contents isn’t necessary, then the ‘Preserve’ reserved word can be omitted.

ReDim fname(7)

Finally, if it is necessary to sort the elements in an array it can be done as follows.

Array.Sort(fname)

Multidimensional Arrays

Multidimensional arrays are useful where you have more than one piece of information, so, to continue with the above example, instead of just storing first names in the array, the last names could also be stored.

Dim names = New String(3, 1) {
    {"Bob", "Smith"},
    {"George", "Jones"},
    {"Fred", "Bloggs"},
    {"Alan", "White"}
}

This is in effect an array of arrays, with each first name and last name pair enclosed in its own set of curly braces. In order to display the names in the console, nested ‘For’ loops need to be used, with the outer loop cycling through the sets of values, in this case, the first name and last name pairs, whilst the inner loop cycles through the values within each set. Within the inner loop it determines whether it is the first name or the last name by using the index number, with the first name having an index number of zero. If the first name is being displayed, then a space is placed after it, whereas if the last name is being displayed, it creates a new line afterwards for the next name.

For nameOuter = names.GetLowerBound(0) To names.GetUpperBound(0)

    For nameInner = names.GetLowerBound(1) To names.GetUpperBound(1)

        If nameInner = 0 Then

            Console.Write(names(nameOuter, nameInner) & " ")

        Else

            Console.Write(names(nameOuter, nameInner) & vbCrLf)

        End If

    Next

Next

The output from this is shown below

Bob Smith
George Jones
Fred Bloggs
Alan White