Partager via


How to: Initialize a Jagged Array 

When you initialize a jagged array variable, you can specify the dimension lengths only for the top-level array. There are several ways you can do this:

To initialize a jagged array variable

  • In the array variable declaration, specify the top-level index upper bound inside the first pair of parentheses, separated by commas. The following example declares and creates a variable to hold a jagged array of Byte elements, specifying only the top-level upper bound.

    Dim rainfall(11)() As Byte
    

    Following the execution of this statement, the array in variable rainfall holds 12 elements, each of which is an empty array of Byte elements.

    -or-

  • In the New clause, specify the top-level index upper bound inside the first pair of parentheses, and supply empty braces ({}). The following example declares and creates a variable to hold a jagged array of Short elements, specifying only the top-level upper bound.

    Dim snowfall()() As Short = New Short(11)() {}
    

    Following the execution of this statement, the array in variable snowfall holds 12 elements, each of which is an empty array of Short elements.

    Note

    You can initialize the top-level index upper bound in only one place. If you specify an upper bound in the parentheses following the array variable name, you cannot use a New clause. If you specify the upper bound in the parentheses in the New clause, you must leave the parentheses following the variable name empty.

    -or-

  • In the New clause, specify the top-level 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 jagged array of Char elements, specifying the top-level upper bound and the values. Note the nested New clauses to initialize the bottom-level arrays.

    Dim decodeValues()() As Char = New Char(1)() {New Char() {"a"c, "b"c}, New Char() {"p"c, "q"c}}
    

    Following the execution of this statement, the array in variable decodeValues holds two elements, each of which is a Char array of length 1 with the element at index 0 holding an initialized value. If you supply both the top-level upper bound and the values, you must include a value for every top-level element from index 0 through the upper bound.

    -or-

  • In the New clause, omit the top-level parentheses and supply the element values inside the braces ({}). The following example declares, creates, and initializes a variable to hold a jagged array of Byte elements, specifying only the element values. Note the two levels of braces in the New clause.

    Dim firstValues()() As Byte = {New Byte() {2, 1}, New Byte() {3, 0}}
    

    Following the execution of this statement, the array in variable firstValues has length 2, with elements firstValues(0) and firstValues(1). Each of the elements is initialized to a two-element Byte array, the first with element values 2 and 1 and the second with element values 3 and 0.

    Note

    Jagged arrays are not compliant with the Common Language Specification (CLS). This means you should not expose jagged arrays from any class you want CLS-compliant code to use.

See Also

Tasks

How to: Declare an Array Variable
How to: Create an Array of Arrays
How to: Initialize an Array Variable
How to: Initialize a Multidimensional Array
Troubleshooting Arrays

Reference

New (Visual Basic)

Concepts

Jagged Arrays in Visual Basic
Writing CLS-Compliant Code

Other Resources

Arrays in Visual Basic