How to: Specify a Zero Lower Bound on an Array

If you are declaring an array, you can specify the lower bound of each dimension using the zero character (0) with the To keyword. This does not change the required lower bound, but it can make your code easier to read.

To explicitly specify the zero lower bound on an array

  1. Declare the array in the normal way.

  2. Inside the parentheses, add 0 To in front of the upper bound for each dimension.

    Public Sub declarelowerbounds()
        Dim monthtotal(0 To 11) As Double
        Dim cell(0 To 39, 0 To 19) As Integer
        MsgBox("Total number of elements:" _
            & vbCrLf & "monthtotal (0 To 11) length " & CStr(monthtotal.Length) _
            & vbCrLf & "cell (0 To 39, 0 To 19) length " & CStr(cell.Length))
    End Sub
    

The lower bound must always be 0, but your code can be more readable when you explicitly declare it. Specifying both bounds also reminds the reader that the lower bound is 0.

Alternative Array Creation. You can create an array without using the Dim Statement (Visual Basic) or the New (Visual Basic) clause. For example, you can call the CreateInstance method, or another component can pass your code an array created in this manner. Such an array can have lower bounds other than 0. You can always test for the lower bound of a dimension with the GetLowerBound method or the LBound Function (Visual Basic).

See Also

Tasks

Troubleshooting Arrays

Concepts

Array Dimensions in Visual Basic

Other Resources

Arrays in Visual Basic