Arrays in Visual Basic (.NET)

Arrays are fundamental data structures in Visual Basic that allow you to store a fixed-size sequence of elements of the same type. They provide a way to manage collections of data efficiently.

What are Arrays?

An array is a group of variables that have the same name and the same type. You can access individual elements within an array using an index, which is a numerical position starting from 0.

Declaring Arrays

You declare an array by specifying its type and then using parentheses () to indicate that it's an array. You can specify the size of the array during declaration or later.

One-Dimensional Arrays

A one-dimensional array is a linear sequence of elements.

Declaring with size:

Dim numbers(9) As Integer ' Declares an array of 10 integers (indices 0 to 9)

Declaring and initializing:

Dim names() As String = {"Alice", "Bob", "Charlie"} ' Declares and initializes an array

Multidimensional Arrays

Visual Basic supports multidimensional arrays, including rectangular (fixed size in all dimensions) and jagged (arrays of arrays) arrays.

Rectangular Arrays:

These are like tables or grids where each dimension has a fixed length.

Dim matrix(2, 3) As Double ' Declares a 3x4 matrix (indices 0-2 for the first dimension, 0-3 for the second)

Jagged Arrays:

These are arrays where each element is itself an array, and the inner arrays can have different lengths.

Dim jaggedArray(2)() As Integer ' Declares an array of arrays
jaggedArray(0) = New Integer() {1, 2}
jaggedArray(1) = New Integer() {3, 4, 5}
jaggedArray(2) = New Integer() {6}

Accessing Array Elements

You access array elements using their index within parentheses.

Example: Accessing elements

Dim scores(4) As Integer ' Indices 0 to 4
scores(0) = 95
scores(3) = 88

Console.WriteLine(scores(0)) ' Output: 95
Console.WriteLine(scores(3)) ' Output: 88

Array Properties and Methods

The System.Array class provides useful properties and methods for working with arrays.

Example: Using Array methods

Dim data() As Integer = {5, 2, 8, 1, 9}
Array.Sort(data) ' Sorts the array

Console.WriteLine("Sorted Array:")
For Each item As Integer In data
    Console.Write(item & " ") ' Output: 1 2 5 8 9
Next
Console.WriteLine()

Console.WriteLine("Length: " & data.Length) ' Output: Length: 5
Console.WriteLine("Rank: " & data.Rank)     ' Output: Rank: 1

Iterating Through Arrays

The For Each...Next loop is a convenient way to iterate through all elements of an array.

Example: Iterating with For Each

Dim cities() As String = {"New York", "London", "Tokyo"}

For Each city As String In cities
    Console.WriteLine(city)
Next
' Output:
' New York
' London
' Tokyo

Array Bounds

An array in Visual Basic is always zero-based by default, meaning the first element is at index 0. The upper bound of an array is one less than its size.

You can use the Option Base 1 statement at the top of your module or class to make arrays 1-based by default, but this is generally not recommended for consistency with the .NET Framework.

Array Constraints

When declaring arrays, the index must be an integral type (like Integer or Long) and must be within the valid bounds of the array.

Attempting to access an array element outside its declared bounds will result in an

IndexOutOfRangeException
.

Further Reading