How to: Initialize an Array Variable in Visual Basic

You initialize an array variable by including an array literal in a New clause and specifying the initial values of the array. You can either specify the type or allow it to be inferred from the values in the array literal. For more information about 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 five lines of code create the same array. 
    ' Preferred syntaxes are on the lines with chars1 and chars2. 
    Dim chars1 = {"%"c, "&"c, "@"c}
    Dim chars2 As Char() = {"%"c, "&"c, "@"c}
    
    Dim chars3() As Char = {"%"c, "&"c, "@"c}
    Dim chars4 As Char() = New Char(2) {"%"c, "&"c, "@"c}
    Dim chars5() As Char = New Char(2) {"%"c, "&"c, "@"c}
    

    After each statement executes, the array that's created has a length of 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 numbers = {{1, 2}, {3, 4}, {5, 6}}
    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 five lines of code create the same array. 
    ' Preferred syntaxes are on the lines with scores1 and scores2. 
    Dim scores1 = {{10S, 10S, 10S}, {10S, 10S, 10S}}
    Dim scores2 As Short(,) = {{10, 10, 10}, {10, 10, 10}}
    
    Dim scores3(,) As Short = {{10, 10, 10}, {10, 10, 10}}
    Dim scores4 As Short(,) = New Short(1, 2) {{10, 10, 10}, {10, 10, 10}}
    Dim scores5(,) As Short = New Short(1, 2) {{10, 10, 10}, {10, 10, 10}}
    

    After each statement executes, 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.

  • The following example iterates through a multidimensional array. In a Windows console application that is written in Visual Basic, paste the code inside the Sub Main() method. The last comments show the output.

    Dim numbers = {{1, 2}, {3, 4}, {5, 6}}
    
    ' Iterate through the array. 
    For index0 = 0 To numbers.GetUpperBound(0)
        For index1 = 0 To numbers.GetUpperBound(1)
            Debug.Write(numbers(index0, index1).ToString & " ")
        Next
        Debug.WriteLine("")
    Next 
    ' Output 
    '  1 2 
    '  3 4 
    '  5 6
    

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 jaggedNumbers = {({1, 2, 3}), ({4, 5}), ({6}), ({7})}
    
    ' Create a jagged array of Byte arrays. 
    Dim images = {New Byte() {}, New Byte() {}, New Byte() {}}
    
  • The following example iterates through a jagged array. In a Windows console application that is written in Visual Basic, paste the code inside the Sub Main() method. The last comments in the code show the output.

    ' Create a jagged array of arrays that have different lengths. 
    Dim jaggedNumbers = {({1, 2, 3}), ({4, 5}), ({6}), ({7})}
    
    For indexA = 0 To jaggedNumbers.Length - 1
        For indexB = 0 To jaggedNumbers(indexA).Length - 1
            Debug.Write(jaggedNumbers(indexA)(indexB) & " ")
        Next
        Debug.WriteLine("")
    Next 
    
    ' Output: 
    '  1 2 3  
    '  4 5  
    '  6 
    '  7
    

See Also

Tasks

Troubleshooting Arrays (Visual Basic)

Other Resources

Arrays in Visual Basic