Troubleshooting Arrays

This page lists some common problems that can occur when working with arrays.

Compilation Errors Declaring and Initializing an Array

Compilation errors can arise from misunderstanding of the rules for declaring, creating, and initializing arrays. The most common causes of errors are the following:

  • Supplying a New (Visual Basic) clause after specifying dimension lengths in the array variable declaration. The following code lines show invalid declarations of this type.

    Dim INVALIDsingleDimByteArray(2) As Byte = New Byte()

    Dim INVALIDtwoDimShortArray(1, 1) As Short = New Short(,)

    Dim INVALIDjaggedByteArray(1)() As Byte = New Byte()()

  • Specifying dimension lengths for more than the top-level array of a jagged array. The following code line shows an invalid declaration of this type.

    Dim INVALIDjaggedByteArray(1)(1) As Byte

  • Omitting the New keyword when specifying the element values. The following code line shows an invalid declaration of this type.

    Dim INVALIDoneDimShortArray() As Short = Short() {0, 1, 2, 3}

  • Supplying a New clause without braces ({}). The following code lines show invalid declarations of this type.

    Dim INVALIDsingleDimByteArray() As Byte = New Byte()

    Dim INVALIDsingleDimByteArray() As Byte = New Byte(2)

    Dim INVALIDtwoDimShortArray(,) As Short = New Short(,)

    Dim INVALIDtwoDimShortArray(,) As Short = New Short(1, 1)

Accessing an Array Out of Bounds

The process of initializing an array assigns an upper bound and a lower bound to each dimension. Every access to an element of the array must specify a valid index, or subscript, for every dimension. If any index is below its lower bound or above its upper bound, an IndexOutOfRangeException exception results. The compiler cannot detect such an error, so an error occurs at run time.

Determining Bounds

If another component passes an array to your code, for example as a procedure argument, you do not know the size of that array or the lengths of its dimensions. You should always determine the upper bound for every dimension of an array before you attempt to access any elements. If the array has been created by some means other than a Visual Basic New clause, the lower bound might be something other than 0, and it is safest to determine that lower bound as well.

Specifying the Dimension

When determining the bounds of a multidimensional array, take care how you specify the dimension. The dimension parameters of the GetLowerBound and GetUpperBound methods are 0-based, while the Rank parameters of the Visual Basic LBound Function (Visual Basic) and UBound Function (Visual Basic) are 1-based.

See Also

Tasks

How to: Declare an Array Variable

How to: Create an Array

How to: Create an Array with More Than One Dimension

How to: Create an Array of Arrays

How to: Create an Array with Mixed Element Types

How to: Create an Array with No Elements

How to: Initialize an Array Variable

How to: Initialize a Multidimensional Array

How to: Initialize a Jagged Array

Other Resources

Arrays in Visual Basic