This documentation is archived and is not being maintained.

9.6.3.3 Array-Size Initializers

Visual Studio .NET 2003

An array-size initializer is a modifier on the name of the variable that gives a set of dimension upper bounds denoted by expressions. For shared and instance variables, the upper-bound expressions must be constant; otherwise the expressions must be classified as values. The upper bound expressions must be implicitly convertible to Integer. The set of upper bounds is equivalent to a variable initializer of an array-creation expression with the given upper bounds. The number of dimensions of the array type is inferred from the array size initializer. So

Module Test
    Sub Main()
        Dim x(5, 10) As Integer
    End Sub
End Module

is equivalent to

Module Test
    Sub Main()
        Dim x As T(,) = new Integer(5, 10) {}
    End Sub
End Module

All upper bounds must be equal to or greater than -1, and all dimensions must have an upper bound specified. If the element type of the array being initialized is itself an array type, the array-type modifiers go to the right of the array-size initializer. For example

Module Test
    Sub Main()
        Dim x(5,10)(,,) As Integer
    End Sub
End Module

declares a local variable x whose type is a two-dimensional array of three-dimensional arrays of Integer, initialized to an array with bounds of 0..5 in the first dimension and 0..10 in the second dimension. It is not possible to use an array size initializer to initialize the elements of a variable whose type is an array of arrays.

If an array element initializer is specified, each expression in the upper bound list must be a constant expression. Furthermore, the rank and dimension lengths specified by the rank initialization list must match those of the array element initializer. A variable declaration may not include both an array type modifier on its type and an array-size initializer.

ArraySizeInitializationModifier ::=
   ( UpperBoundList ) [ ArrayTypeModifiers ]
UpperBoundList::=
   Expression |
   UpperBoundList , Expression

See Also

9.6.3.1 Regular Initializers | 9.6.3.4 Array-Element Initializers | 9.6.3.2 Object Initializers | 4.6 Accessibility | 4.3.3 Shadowing | 9.6 Instance and Shared Variables | 9.6.1 Read-Only Variables | 9.6.2 WithEvents Variables | 9.6.3 Variable Initializers | 11. Expressions | Declarations and Constants Summary (Visual Basic Language Reference) | Variables (Visual Basic Language Concepts)

Show: