Arrays and Array Declaration in Visual Basic .Net

In Visual Basic an array can be defined as number of memory locations, in which each memory location can store the same data type. And each value can be referenced through a same variable. An array is a sequential collection of values of same datatype where the collection has a fixed size.

Arrays are consist of contiguous memory locations. Where the lowest address corresponds to the first element and the highest address to the last element.

Declaring Arrays in VB.Net

Dim array_name(array_size) As Data-type

Example

Dim array1(12) As integer 
'To declare an array array1 as integer

Dim array2(20) As String
'To declare an array array2 as String

Dim array3(10,13) As Integer
'To declare a two dimensional array array3

Initializing the array elements along with declaration

Dim intArray ( ) As Integer = {12, 16, 20, 24, 28, 32}
Dim stringArray ( ) As String = {"Adeeb", "TECHAntena"}
Dim objectArray ( ) As Object = {"TECHAntena", 12, u40, 56A, "Adeeb"}

Example program to assign array values using array index

Module Module1

 Sub Main()
   Dim numArray(20) As Integer
   Console.WriteLine("Enter the array limit")
   Dim limit As Integer = Console.ReadLine()

   'Initilizes the array values
   For i = 0 To limit
     numArray(i) = i
   Next i

   'Print the array values
   Console.WriteLine("The Array is:")
   For j = 0 To limit
    Console.WriteLine(numArray(j))
   Next

   Console.ReadKey()
 End Sub

End Module

If you input the limit 10 then the output of the above program will be as shown below.

vb arrays

array initialization vb.net

Dynamic Arrays

We can use the Dim statement to declare dynamic array without specifying the array size. Dynamic arrays can be dimensioned or re-dimensioned as we need using the ReDim statement.

Syntax

ReDim [Preserve] arrayname(subscripts)

Where Preserve is a keyword used to preserve the data in an existing array, when we resize it and the subscripts is the new dimension of the array.

Example Program

Module Module1

  Sub Main()
    Dim num() As Integer
    ReDim num(2)
    num(0) = 1
    num(1) = 2
    ReDim Preserve num(10)
    num(2) = 3
    num(3) = 4
    num(4) = 5
    num(5) = 6
    num(6) = 7
    num(7) = 8
    num(8) = 9
    num(9) = 10

    For i = 0 To 9
      Console.WriteLine(i & vbTab & num(i))
    Next i
    Console.ReadKey()
  End Sub

End Module

The output of the above program will be as follows.

0  1
1  2
2  3
3  4
4  5
5  6
6  7
7  8
8  9
9  10

Multi-Dimensional Arrays

Visual Basic supports the multi dimensional arrays. Multi dimensional arrays are also called rectangular arrays. The simplest form multi dimensional arrays is two-dimensional arrays.

Example – Declaring Multi Dimensional arrays

'Declare two dimensional array
Dim two_dim  (10, 5) As Integer

'Declare three dimensional array
Dim three-dim (10, 20, 30) As Integer

Jagged Array

The array of arrays is called Jagged Array.

Declaration

Dim num As Integer()() = New Integer(10)(){}

Example

Module Module1

Sub Main()
 'This is a jagged array of 5 array of integers
 Dim a As Integer()() = New Integer(4)() {}
 a(0) = New Integer() {0, 0}
 a(1) = New Integer() {1, 2}
 a(2) = New Integer() {2, 4}
 a(3) = New Integer() {3, 6}
 a(4) = New Integer() {4, 8}
 
Dim i, j As Integer
 ' output each array element's value 
 For i = 0 To 4
    For j = 0 To 1
      Console.WriteLine("a[{0},{1}] = {2}", i, j, a(i)(j))
    Next j
 Next i
 
Console.ReadKey()
 End Sub

End Module

The output of the above example is as shown below.

a[0][0]: 0
a[0][1]: 0
a[1][0]: 1
a[1][1]: 2
a[2][0]: 2
a[2][1]: 4
a[3][0]: 3
a[3][1]: 6
a[4][0]: 4
a[4][1]: 8

The array manipulation and classes are described in the next tutorial.