Dela via


How to: Initialize an Array Variable

You can initialize an array at the same time that you create it, as part of the New (Visual Basic) clause. You can also initialize it in subsequent assignment statements.

The aspects of an array that you can initialize are as follows:

  • The index upper bounds, which specify the lengths of the array's dimensions

  • The values of some or all of the array's elements

You can initialize either of these without initializing the other. However, if you supply element values but not upper bounds, the number of values that you supply determines the upper bounds.

To initialize an array in the New clause at creation time

  • In the New clause, specify the 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 an array with elements of the Char Data Type (Visual Basic), specifying the upper bound and the values.

    Dim testChars As Char() = New Char(2) {"%"c, "&"c, "@"c}
    

    Following the execution of this statement, the array in variable testChars has length 3, with elements at index 0 through index 2 holding initialized 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.

    Note the literal type character c, which forces a character literal to the Char data type. By default, without any type character, a literal enclosed in quotation marks (" ") uses String.

    You do not have to specify the index upper bound if you supply element values in the New clause. The following example declares, creates, and initializes a variable to hold an array with elements of the Boolean Data Type (Visual Basic), specifying only the element values.

    Dim answers As Boolean() = New Boolean() {True, True, False, True}
    

    Following the execution of this statement, the array in variable answers has length 4, with elements at indexes 0 through 3 holding initialized values.

You can initialize the index upper bound without initializing any elements. If you create an array in this manner, you must use a subsequent assignment statement to initialize each element value.

To initialize an array in subsequent assignment statements

  1. Specify the index upper bound in the array variable declaration.

  2. Use one or more assignment statements, each of which assigns a value to one of the array's elements. The following example declares and creates a variable to hold an array with elements of the String Data Type (Visual Basic), and supplies two element values in subsequent statements.

    Dim comments(30) As String
    comments(0) = "This is the first comment."
    comments(5) = "This is the sixth comment."
    

    Following the execution of these statements, the array in variable comments has length 31, with elements at indexes 0 and 5 holding initialized values, and the other 29 elements holding default values. If you initialize an array in this manner, you can initialize some elements and skip over others.

-or-

  • Use the ReDim Statement (Visual Basic) to initialize the array's length.

    ReDim comments(5)
    

    Following the execution of this statement, the array in variable comments has length 6, with all the elements holding default values.

    Note

    You can initialize the index upper bound in only one location. If you specify an upper bound in the parentheses that follow 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 that follow the variable name empty.

See Also

Tasks

How to: Declare an Array Variable

How to: Create an Array

How to: Initialize a Multidimensional Array

How to: Initialize a Jagged Array

Troubleshooting Arrays

Concepts

Overview of Arrays in Visual Basic

Other Resources

Arrays in Visual Basic