Sdílet prostřednictvím


How to: Initialize Array Variables in Visual Basic

You can use an array literal to populate an array with initial values when it is created. You can include the array literal as part of the New clause and explicitly specify the type. If you do not specify a type of the array elements, the type is inferred from the values in the array literal. For details on how the type is inferred, see "Populating an Array with Initial Values" in Arrays in Visual Basic.

To initialize an array variable by using an array literal

  • Either in the New clause, or when you assign the array value, supply the element values inside braces ({}). The following example shows several ways to declare, create, and initialize a variable to contain an array that has elements of type Char.

    ' The following three lines of code create the same array.
    Dim chars1() As Char = New Char(2) {"%"c, "&"c, "@"c}
    Dim chars2 = {"%"c, "&"c, "@"c}
    Dim chars3() As Char = {"%"c, "&"c, "@"c}
    

    Following the execution of these statements, the array created has length 3, with elements at index 0 through index 2 containing the initial values. If you supply both the upper bound and the values, you must include a value for every element from index 0 through the upper bound.

    Notice that you do not have to specify the index upper bound if you supply element values in an array literal. If no upper bound is specified, the size of the array is inferred based on the number of values in the array literal.

To initialize a multidimensional array variable by using array literals

  • Nest values inside braces ({}) within braces. Ensure that the nested array literals all infer as arrays of the same type and length. The following code example shows several examples of multidimensional array initialization.

    Dim ticTacToe = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}
    Dim customerData = {{"City Power & Light", "http://www.cpandl.com/"},
                        {"Wide World Importers", "http://wideworldimporters.com"},
                        {"Lucerne Publishing", "http://www.lucernepublishing.com"}}
    
    ' You can nest array literals to create arrays that have more than two 
    ' dimensions.
    Dim twoSidedCube = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}
    
  • You can explicitly specify the array bounds, or leave them out and have the compiler infer the array bounds based on the values in the array literal. 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. The following example shows several ways to declare, create, and initialize a variable to contain a two-dimensional array that has elements of type Short

    ' The following three lines of code create the same array.
    Dim startingScores1(,) As Short = New Short(1, 2) {{10, 10, 10}, {10, 10, 10}}
    Dim startingScores2 = {{10S, 10S, 10S}, {10S, 10S, 10S}}
    Dim startingScores3(,) As Short = {{10, 10, 10}, {10, 10, 10}}
    

    Following the execution of this statement, the created array contains six initialized elements that have indexes (0,0), (0,1), (0,2), (1,0), (1,1), and (1,2). Each array location contains the value 10.

To initialize a jagged array variable by using array literals

  • Nest object values inside braces ({}). Although you can also nest array literals that specify arrays of different lengths, in the case of a jagged array, make sure that that the nested array literals are enclosed in parentheses (()). The parentheses force the evaluation of the nested array literals, and the resulting arrays are used as the initial values of the jagged array. The following code example shows two examples of jagged array initialization.

    ' Create a jagged array of arrays that have different lengths.
    Dim diagonal = {({0, 0, 0}), ({0, 0}), ({0})}
    
    ' Create a jagged array of Byte arrays.
    Dim images = {New Byte() {}, New Byte() {}, New Byte() {}}
    

See Also

Tasks

Troubleshooting Arrays (Visual Basic)

Other Resources

Arrays in Visual Basic