Arrays

Arrays are lots or variables or the same type stored in sequence. Each variable in the Array is given a unique reference number. Arrays are useful if you are going to use a large number of variables of the same type for a similar purpose. Arrays are declared by putting the arrays length in brackets at the end of the variable name eg.

Dim Number(5) As Integer
Dim Address(20) As String

When I said the length goes in brackets I wasn't actually being correct. In fact when you put the value in you are actually saying that the array should stretch from 0 to the value. This means you have one more element in your array than the number you typed. So if you declare 'Dim x(3) As Integer' you will have these array elements:

x(0)
x(1)
x(2)
x(3)

You are able to change the starting value by declaring 'Dim x(1 to 3) As Integer'.

To set a value to an element of an array you simply you the arrays name followed by the elements id(number) in the array. This is treated like an ordinary variable.

Multidimensional Arrays

Multidimensional arrays are arrays that are defined with more than one element id. For example a Noughts and Crosses board to use a two dimensional array for the columns and rows.

Dim board(2,2) As Byte

We would the get these nine elements:

board(0,0)
board(0,1)
board(0,2)
board(1,0)
board(1,1)
board(1,2)
board(2,0)
board(2,1)
board(2,2)

The elements are in fact stored in that order in the array however this does not really affect us using it. As you can imagine multidimensional arrays can easily become very large. If you think about having a multidimensional array to store RGB colour values to save multiplying them every time would have to be (255, 255, 255). This produces 16,581,375 elements and would obviously be completely impractical.

Dynamic Arrays

This is one of the features that you will probably not use much when you first begin to program but as you gain experience you will find you use it more and more. Dynamic arrays are arrays which we can change the length of as the program runs. This is unlike static arrays where you must declare the lengths while you are writing the program.

You first declare dynamic arrays like this:

Dim MyArray() As String

This tells Visual Basic not to set a length to the array and make it dynamic. To set the length of the aray we use the ReDim statement. when we use the ReDim statement we do not use As because we have already set the data type.

ReDim MyArray(5)

We would usually be using a variable instead of five as it would be just as well to use a static if we know its length. The problem with the plain ReDim statement is that if there is already data inside of the array then it will be wiped when we change the length. To stop this we must use the Preserve keyword like this:

Redim Preserve MyArray(5)

The Preserve keyword leaves all of the data intact in the array as long as you have not declared the array to a smaller value and removed its space.

UBound and LBound
This two functions give us the lower and upper boundaries of the array. We use it like this:

MyVar = UBound(MyArray)

This will set the value of MyVar to the upper boundary of the array. We know know the largest value we can use to reference an element of the array.

© Jonathan Waller 2005; QuantumState Visual Basic