VB.Net Arrays VB.NET

The array data type is a fundamental data type in most languages, including Visual Basic. An array is used to store a collection of similar data types or objects.

Many authors of programming books misuse the terms associated with arrays, so let's begin by establishing the correct terminology. In fact, if you will indulge us, we would like to begin with a formal definition of the term array.

Definition of Array

Let S1, S2 ..., SN be finite sets, and let T be a data type (such as Integer). Then an array of type T is a function:

where S1 x S2 x ... x SN is the Cartesian product of the sets S1, S2 ..., SN. (This is the set of all n-tuples whose coordinates come from the sets Si.)

For arrays in VB .NET (and the other languages that implement the Common Language Runtime), the sets Si must have the form:

In other words, each set Si is a finite set of consecutive integers starting with 0.

Each position in the Cartesian product is referred to as a coordinate of the array. For each coordinate, the integer Ki is called the upper bound of the coordinate. The lower bound is 0 for all arrays in VB .NET.

Dimension of an Array

The number N of coordinates in the domain of the function arr is called the dimension (or sometimes rank) of the array. Thus, every array has a dimension (note the singular); it is not correct to refer to the dimensions of an array (note the plural). An array of dimension 1 is called a one-dimensional array, an array of dimension 2 is called a two-dimensional array, and so on.

Size of an Array

Along with a dimension, every array has a size. For instance, the one-dimensional array:

has size 6. The two-dimensional array:

has size 6x9. The three-dimensional array:

Arrays in VB .NET

In VB .NET, all arrays have lower bound 0. This is a change from earlier versions of VB, where we could choose the lower bound of an array.

The following examples show various ways to declare a one-dimensional array:

Note that an array declaration can:

• Call the array's constructor implicitly or explicitly. (The constructor is the function that VB .NET uses to create the array.)
• Specify an initial size for each dimension or leave the initial size unspecified.
• Initialize the elements of the array or not.

It is important to note that in the declaration:

the number X is the upper bound of the array. Thus, the array elements are ArrayName(0) through
ArrayName(X), and the array has X+1 elements.
Multidimensional arrays are declared similarly. For instance, the following example declares and
initializes a two-dimensional array:

and the following code displays the contents of the array:

In VB .NET, all arrays are dynamic: there is no such thing as a fixed-size array. The declared size should be thought of simply as the initial size of the array, which is subject to change using the ReDim statement. Note, however, that the dimension of an array cannot be changed.

Moreover, unlike with VB 6, the ReDim statement cannot be used for array declaration, but can be used only for array redimensioning. All arrays must be declared initially using a Dim (or equivalent) statement.

Redimensioning arrays

The ReDim statement is used to change the size of an array. This is referred to as redimensioning – a term no doubt invented by someone who didn't know the difference between the dimension of an array and the size of an array! In any case, redimensioning changes the size of the array, not its dimension. In fact, as we have already mentioned, the dimension of an array cannot be changed.

The UBound function returns the upper limit of an array coordinate. Its syntax is:

UBound(MyArray, CoordinateIndex)

where CoordinateIndex is the index of the coordinate for which we want the upper bound.
Here is an example of array redimensioning:

When an array is redimensioned using the ReDim statement without qualification, all data in the array is lost; that is, the array is reinitialized. However, the Preserve keyword, when used with ReDim, redimensions the array while retaining all current values. Note that when using the Preserve keyword, only the last coordinate of an array can be changed. Thus, referring to the array defined earlier, the following code generates an error:

You will probably not be surprised to learn that redimensioning an array is a time-intensive process. Hence, when redimensioning, we face the ubiquitous dichotomy between saving space and saving time. For instance, consider the code segment used to populate an array:

The key issue here is to decide how much to increase the size of the array each time resizing is necessary. If we want to avoid using any extra space, we could increase the size of the array by each time:

But this would be very inefficient. Alternatively, we could kick up the size by 1,000:

But this uses a lot of extra space. Sometimes experimentation is required to find the right compromise between saving space and saving time.