Visual Basic Array – A Detailed Study

visualbasicarrayIntroduction to Array in VB.NET

Concept of an array is quite simple across the languages and it is no different in Visual Basic. Let us learn the concept of Array in detail for VB.NET language.

An array is a memory storage location that holds a sequential collection of the same data type. Arrays are built with contiguous memory addresses. The lowest location address corresponds to array’s first element and the highest location address to the Array’s last element. One important thing to note for user is that array’s index always starts from zero (0) and not 1. i.e. array[0] would return array’s first element and not array[1].

Here is a representation of an array:

First Array Element                                                                             Last Array Element

Array[0]

Array[1]

Array[2]

Array[3]

Array[4]

Creating Array in VB.NET

Let us now look at few examples of declaring arrays in VB.NET:

In order to declare an array in VB.NET, Dim statement is being used as shown below:

  1. Dim intElements(10) – Declaration of an array with 11 Elements

  2. Dim strElements(25) As String – Declaration of an array with 26 string Elements

  3. Dim twoDimentsional(15, 25) As Integer – Declaration of a two dimensional (or multi-dimensional) array of integer elements

  4. Dim multiDArray(10, 100, 50) – Declaration of a three dimensional (or multi-dimensional) array

Let us now understand above declarations of an array in the same index as they have been declared:

  1. In the first example, a simple array of the variable type has been declared which can hold 11 elements of any type.

  2. The second example contains a declaration of a simple array of string type which can hold 26 string elements

  3. The third example shows the two-dimensional array of integer data type. In such arrays, data can be saved in rows and columns matrix. Arrays with more than one-dimension are also known as multi-dimensional arrays.

  4. The fourth case is an example of a three-dimensional array with generic data type and as mentioned above, it falls under the category of multi-dimensional array.

Array Declaration with Initialization:

Array can be initialized while being declared, as shown in the below example:

Dim intElements() As Integer = {2671, 5724, 4879, 1478}

Dim strFriends() As String = {“John”, “Roger”, _

“Mark”, “Shane”, “Peter”}

Dim EtcElements() As Object = {“Hello”, 78a, *4e9, “V”s}

In all above examples, arrays have initialized with the values with their declarations.

Using Array Index

The usual method to store and access the elements in an array is through array index.

Here is an example which shows the same:

Module arrayIndex
  Sub Main()
  Dim n_arr(4) As Integer  ' Declaring an array of 5 elements
  Dim m, n As Integer
  'Initializing array elements     
  For m = 0 To 4
      n_arr(m) = m + 10                'store elements at index m to m + 10
  Next m
  'Fetch values of each array index'
  For n = 0 To 4
      Console.WriteLine("Index({0}) = {1}", n, n_arr(n))
  Next n
  Console.ReadKey()
  End Sub
End Module

Output:

Here is an output of above code:

Index(0) = 10

Index(1) = 11

Index(2) = 12

Index(3) = 13

Index(4) = 14

Multi-Dimension Array

Any Array with more than one dimension is known as multi-dimensional array. As mentioned earlier, implementation of Multi-Dimensional array is possible with Visual Basic. Additionally, you can even refer the multi-dimensional arrays as rectangular arrays.

Here are exemplary declarations of two and three dimensional arrays:

2-dimensional array:

Dim multiDArray(5, 10) As Integer

3-dimensional array:

Dim multiDArray(5, 5, 5) As string

Let us look at the example of two-dimensional array as multi-dimensional array:

Module arrayIndex
  Sub Main()
  ' Declaration of an array with  2 dimensions, 3 rows - 2 columns
  Dim multiDArray(,) As Integer = {{5, 10}, {15, 20}, {25, 30}}
  Dim m, n As Integer
  'Fetch elements from each index of an array'
  For m = 0 To 2
      For n = 0 To 1
          Console.WriteLine("multiDArray [{0},{1}] = {2}", m, n, multiDArray (m, n))
      Next n
  Next m
  Console.ReadKey()
  End Sub
End Module

Output:

Here is an output of above code

multiDArray [0,0]: 5

multiDArray [0,1]: 10

multiDArray [1,0]: 15

multiDArray [1,1]: 20

multiDArray [2,0]: 25

multiDArray [2,1]: 30

Dynamic Arrays

Till now, we saw the arrays, which were static arrays, meaning their size was determined at the time of declaration. However, VB.NET also supports the Dynamic arrays. As the name suggests, dynamic arrays are those arrays which can be dimensioned as well as re0dimensiones as required. Dynamic arrays can be declared with the ReDim statement in VB.NET

Let us look at general syntax of the Dynamic array:

ReDim [Preserve] <name_of_array>(re-dimension size)

Where,

  • The Preserve is a keyword which can help preserve data into an existing array, while resizing the array.

  • <name_of_array> is the array name to re-dimension or re-size.

  • re-dimension size specifies the new dimension for the array.

Here is an example of dynamic array:

Module arrayApl
  Sub Main()
  Dim EmpId() As Integer
  ReDim EmpId(2)
  EmpId(0) = 2182
  EmpId(1) = 3241
  EmpId(2) = 1475
  ReDim Preserve EmpId (5)
  EmpId (3) = 7312
  EmpId(4) = 9424
  EmpId(5) = 3791
  EmpId(6) = 2486
  EmpId(7) = 1573
For n = 0 To 5
      Console.WriteLine(i & vbTab & EmpId(n))
  Next n
  Console.ReadKey()
  End Sub
End Module

Output:

Here is an output of above example program.

0       2182

1       3241

2       1475

3       7312

4       9424

5       3791

6       2486

7       1573

Array Class and its Properties

Array class in VB.NET is a base call for all other arrays. The array class I defined in System namespace. There are various array properties of working with array as listed below:

  1. IsFixedSize – To check if array is of fixed size

  2. IsReadOnly – To check if array has read-only right

  3. Length – To check length (or size) of an array

  4. LongLength – 64-bit integer to know total elements in array

  5. Rank – To know the dimensions of an array