# Visual Basic/Arrays
## Introduction

Arrays are extremely useful in Visual Basic, and are present in many other programming languages. Arrays are used to group similar data together, to make it easier to search and sort through this data. The best way to look at an array is to show you one. Say you want to make a phone book for your computer. Rather than make a new variable with a new name whenever you want to add a name, you can make an array. Arrays consist of the variable name and a subscript, which is put in parentheses for computer use. So, if you want to make a phone book of say, 100 people, rather than make a bunch of variables named 'person1, person2, person3....person100', you may use an array. All you have to do is dimension the array (see above). So now those 100 variables turn into person(1 to 100) in a dim statement. Now you may be asking, what do I do now? Each object stored in an array is called an element. What you have done is created an array which can hold 100 elements. If you want to print the 32nd person in your phone book, you simply would type:

## Use of Arrays

Arrays have far more uses other than just making phone books. But before we get into specific uses, let's look at some of the basic techniques used when working with arrays. The first thing you may want to do is learn how to easily fill an array. Filling an array with random numbers is a popular technique for starting off programs, and testing other techniques such as sorting. To fill an array, a simple For Loop may be used.
The above shows an example of read-write iteration over an array. For Each loop can be used for read-only iteration over an array:

## Indices

Per default, array indices start at 0, unless "Option Base 1" declaration is used. Without the declaration used, an array declared as "Dim MyArray(5)" has 6 elements: 0, 1, 2, 3, 4, 5.
The index range of an array has to be a continuous sequence of integers, including negative numbers. Thus, the following is possible:

## Size

The size of an array can be obtained using LBound and UBound as follows:
Keywords: length, item count, element count.

## Dynamic Arrays

An array with the number of elements specified upon its declaration, as in Dim Names(0 to 9), is a static one: the number of its elements cannot be changed in runtime. By contrast, an array declared without the number of elements, as in Dim Names, is a dynamic array, and its number of elements can be changed using ReDim. To preserve the element content of the array when using ReDim, Preserve keyword has to be used after ReDim.
Say you have a phone book program running and it has an array of your friends' names, like this:
But say you want to add more friends to your phone book, when a button is clicked, for example. What do you do? You can define a dynamic array instead:
This array initially has no elements. You can add more elements, by using the ReDim keyword:
This would create two new elements. You can assign names to these elements in the same way as before:
If you want to add more entries then use, for example:
But you'll end up losing all the old records! If you want to keep them, you must also use the Preserve keyword:
To iterate AKA loop over the elements of a dynamic array, you have to use LBound and UBound functions or For Each loop, as described at #Use of Arrays.
To add an element to an initialized dynamic array, you can proceed as follows, using UBound function:
However, UBound does not work with an un-initialized dynamic array, so the following would cause a run-time error:
One way to avoid this run-time error is to keep a flag:
Another way to avoid the run-time error is to catch the error created by the use of "UBound" on an uninitialized array:
Finally, you can use the following hack to check if a dynamic array has been initialized:

## Variant Arrays

Variant arrays are dynamic arrays declared using the Variant type, and initialized using "= Array". Their advantage is that, after they are initialized using "= Array", LBound and UBound functions work with them even when they have no elements, as follows:
As a consequence, adding an element is straightforward, with no need to check for UBound failure:
Variant arrays can be initialized to a set of values:
For very large arrays, the overhead of using variant arrays, which have the element type of Variant rather than String or other narrower type, may well be restrictive, in which case conventional dynamic arrays should be used. What seems also much faster than variant arrays for adding items one at a time are Collections.

## Multi-Dimensional Arrays

Arrays can be defined to have any number of dimensions (or indices), by listing the sizes of each dimension:
An element can be referenced like this:
Dynamic arrays can also be re-dimensioned to have any number of dimensions:
The LBound and UBound functions can be used to find the bounds of a particular dimension:
Would give 4.
Using UBound without the second parameter gives the first dimension

## Erasing Arrays

Any type of array can be re-set to empty by using:

## Mixing Arrays

The real power of arrays comes when defining arrays of arrays. What does this mean? Declare an array:
What do we have here? Essentially two arrays inside another. They can be referenced like this:
Would show 3.
You can nest arrays like this to any depth and in any order and they can be of any size. A note of caution must be taken when using the ReDim statement, however. You cannot specifically re-dimension a particular dimension of an array; instead you need to temporarily copy to a variant.

## Use of Matrices

See also the section #Multi-Dimensional Arrays.
Matrices are not as commonly used as arrays, but are an important element of programming. Rather than just one dimension, a matrix may have 2 or more. So, to make a matrix, the Dim statement would be:
This will create a matrix that is 10 by 10 and comprised of integers. In reality, a matrix is much like an array of arrays. The first thing you'll want to do is know how to create a loop for reading and filling a matrix. But before that, even, you should know how matrices are structured. An example matrix would be:
This is a 4 by 9 matrix. When talking about matrices, rows are always stated before columns. You will also notice column numbers travel from left to right, while row numbers travel from top to bottom. This is very important. And of course, matrices need not consist of merely 1s. So, now you want to fill a matrix? Its very similar to an array.
This will of course fill the matrix with nothing but ones, but random numbers may be used (see references). As you may notice, this involves nested loops. The loop fills left to right, top to bottom (the same directions we use when reading).

## Sorting

For sorting variant arrays, see http://stackoverflow.com/questions/152319/vba-array-sort-function.
| Previous: Strings | Contents | Next: Files |