An array is a set of values that are logically related to each other. You refer to these values by the same variable name and use a number called an index or subscript to select each value. These values are known as the elements of the array and fully use a range from zero thru a specified maximum.

Variables other than arrays are known as scalars. A scalar can hold only one value at a time.

To make an array

You make an array with the same statements you use to make other variables. The only difference is the dimensions of the array, which you put in parentheses. Some examples follow.

Dim my_first_array(10)

Because my_first_array uses one index or subscript, it is known as a onee-dimensional array. You also can make arrays with more than one subscript, usually known as multidimensional arrays. See Array Dimensions for more information.

To store values in an array

You get access to each element in an array with an index of type Int. You write the name of the array, then the index between parentheses. Indexes for multidimensional arrays have a comma between each. You use one index for each array dimension.

To give an array initial values

You can give an array some initial values with an array initializer. An array initializer is a list of values with a comma (,) between each, all between braces ({ }).

Dim days() = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }

Nested initializers

You can make arrays with many dimensions with nested array initializers.

Dim magic() = {{4, 9, 2}, {3, 5, 7}, {8, 1, 6}}

To iterate through an array

When you iterate through an array, you do something to each item in the array. You can go from lowest to highest or highest to lowest. Typically you use one of the two statements For or For Each. If you do not know the limits of the array, then you can call the method Last to get the highest index value.

The example that follows uses the statement For to iterate through an array with one dimension.

Dim numbers() As Int = { 1, 2, 3 }

For index = 0 To numbers.Last(0)
    PrintLine numbers(index)
End For

The example that follows uses the statement For to iterate through an array with two dimensions.

Dim numbers(*2) = {{1, 2}, {3, 4}, {5, 6}}

For row = 0 To numbers.Last(0)
    For column = 0 To numbers.Last(1)
        Print numbers(row, column) & $32
    End For
End For
1 2
3 4
5 6

The example that follows uses the statement For Each to iterate through arrays with one and two dimensions.

Dim numbers1D() As Int = { 1, 2, 3 }

For Each number In numbers1D
    PrintLine number
End For

Dim numbers2D(*2) As Int = {{4, 5}, {6, 7}, {8, 9}}

For Each number In numbers2D
   PrintLine number
End For

Arrays as return values and parameters


Dim my_numbers() = GetNumbers()
ShowNumbers my_numbers

Sub GetNumbers() As Int*1
    Dim numbers() As Int = { 119, 111, 114, 108, 100 }
    Return numbers
End Sub

Sub ShowNumbers(numbers(*1) As Int)
    For Each number In numbers
        PrintLine number
    End For
End Sub

Array dimensions


See also