Partager via


How to: Declare an Array Variable 

You declare an array variable the same way as any other variable, using the Dim statement. You follow the variable name with one or more pairs of parentheses to indicate that it is to hold an array rather than a scalar (a variable containing a single value).

Declaring Array Variables

To declare a one-dimensional array variable

  • In your declaration, add one pair of parentheses after the variable name. The following example declares a variable to hold a one-dimensional array with elements of the Double Data Type (Visual Basic).

    Dim cargoWeights() As Double
    

    The preceding example declares an array variable but does not assign an array to it. You still must create a one-dimensional array, initialize it, and assign it to cargoWeights.

To declare a multidimensional array variable

  • In your declaration, add one pair of parentheses after the variable name and place commas inside the parentheses to separate the dimensions. The following example declares a variable to hold a four-dimensional array with elements of the Short Data Type (Visual Basic).

    Dim atmospherePressures(,,,) As Short
    

    The preceding example declares an array variable but does not assign an array to it. You still must create a four-dimensional array, initialize it, and assign it to atmospherePressures.

To declare a jagged array variable

  • In your declaration, add as many pairs of parentheses after the variable name as there are levels of nested arrays. The following example declares a variable to hold an array of arrays of arrays (an array, each element of which is an array, each element of which is an array), with the innermost array having elements of the Byte Data Type (Visual Basic).

    Dim inquiriesByYearMonthDay()()() As Byte
    

    The preceding example declares an array variable but does not assign an array to it. You still must create an array of arrays of arrays, initialize it, and assign it to inquiriesByYearMonthDay.

See Also

Tasks

How to: Create an Array
How to: Create an Array with More Than One Dimension
How to: Create an Array of Arrays
How to: Initialize an Array Variable
Troubleshooting Arrays

Concepts

Overview of Arrays in Visual Basic
Multidimensional Arrays in Visual Basic
Jagged Arrays in Visual Basic

Other Resources

Arrays in Visual Basic