Share via


How to: Initialize a Multidimensional Array

You can initialize a multidimensional array variable the same ways you can initialize a one-dimensional array, except that you must account for every dimension.

To initialize a multidimensional array variable

  • In the array variable declaration, specify each index upper bound inside the parentheses, separated by commas. The following example declares and creates a variable to hold a two-dimensional array with elements of the Short Data Type (Visual Basic), specifying only the upper bounds.

    Dim sizes(1, 1) As Short
    

    Following the execution of this statement, the array in variable sizes has a total of four elements, at indexes (0, 0), (0, 1), (1, 0), and (1, 1), holding default values. If you create an array in this manner, you must use a subsequent assignment statement to assign each element value.

    -or-

  • Follow the declaration with an equal sign (=) and a New (Visual Basic) clause. In the New clause, repeat the element data type, specify the index upper bounds inside parentheses, and supply empty braces ({}). The following example declares and creates a variable to hold a three-dimensional array with elements of the Short data type, specifying only the upper bounds.

    Dim replyCounts(,,) As Short = New Short(2, 1, 2) {}
    

    Following the execution of this statement, the array in variable replyCounts has 18 elements holding default values. If you create an array in this manner, you must use a subsequent assignment statement to assign each element value.

    Note

    You can initialize the index upper bounds in only one location. If you specify upper bounds in the parentheses following the array variable name, you cannot use a New clause. If you specify upper bounds in the parentheses in the New clause, you must leave the parentheses following the variable name empty.

    -or-

  • In the New clause, specify each index upper bound inside the parentheses, and supply the element values inside the braces ({}). The following example declares, creates, and initializes a variable to hold a two-dimensional array with elements of the Short data type, specifying the upper bounds and the values. Note the two levels of braces in the New clause.

    Dim startingScores(,) As Short = New Short(1, 1) {{10, 10}, {10, 10}}
    

    Following the execution of this statement, the array in variable startingScores holds four initialized elements. If you supply both the upper bounds and the values, you must include a value for every element from index 0 through the upper bound in every dimension.

    -or-

  • In the New clause, leave the parentheses empty except for commas for the appropriate number of dimensions, and supply the element values inside the braces ({}). The following example declares, creates, and initializes a variable to hold a two-dimensional array with elements of the Single Data Type (Visual Basic), specifying only the element values. Note the two levels of braces in the New clause.

    Dim diagonal(,) As Single = New Single(,) {{1, 0}, {0, 1}}
    

    Following the execution of this statement, the array in variable diagonal holds four initialized elements.

See Also

Tasks

How to: Declare an Array Variable

How to: Create an Array with More Than One Dimension

How to: Initialize an Array Variable

How to: Initialize a Jagged Array

Troubleshooting Arrays

Concepts

Multidimensional Arrays in Visual Basic

Other Resources

Arrays in Visual Basic