VB.NET Arrays


  • Arrays are the collection of similar data types.
  • An Array is a set of values that are logically related to each other.
  • The individual values of an array are called the elements of array.
  • These elements are stored in array with the index values starting with 0 and ending with the value one less than the size of array.
  • Array consists of the contiguous memory locations. Lower address memory location represents first element and highest address memory location represents last element.
  • array

Declaration of an Array


Declaration of Array is done using the following syntax:


  • Dim keyword is used to declare an array.
  • array-name can be any meaningful name given to the array.
  • size of an array determines the number of elements present in the array.
  • data-type represents the data-types of elements of an array.
  • Here are some examples of declaration of an array:
  • Dim highestmarks(20) As Double
    Dim name(20) As String
    Dim length(10) As Integer

Initialization of an Array


You can also initialize an array element while declaring the array:


Dim name() As String={“Ram”,”Siya”,”Manoj”,”Shivam”}
Dim length() As Integer={10,20,25,67}

Following are the basic type of array in VB.NET:


  • One-Dimensional Array
  • Multi-Dimensional Array
  • Jagged Array
  • Dynamic Array

One-Dimensional Array


  • An array which contains only one dimension is known as one dimensional array.
  • One-Dimensional array is a simplest type of array.
  • It is declared as the following syntax which we have already discussed:
  • Dim array-name (size) As data-type

  • Here is an example of one-dimensional array:
  • Example

    Module Module1
    Sub Main()
    Dim length() As Integer = {1, 2, 3, 4, 5}
    Dim sum As Integer
    sum=0
    For i = 0 To 4
    sum = sum + length(i)
    Next i
    Console.WriteLine("Sum of elements of array is:{0}", sum)
    Console.ReadLine()
    End Sub
    End Module

  • The output of the above program will be as follows:
  • Output

    Sum of elements of array is:15

Multi-Dimensional Array


  • An array which contains more than one dimension is known as Multidimensional array. The simplest form of the multi dimensional array is two-dimensional array.
  • You can declare a two-multidimensional array as follows:
  • Dim length(3,4) As Integer

  • You can also declare a three-dimensional array as follows:
  • Dim name(10,15,8) As String

  • Here is an example of two-dimensional array:
  • Example

    Module Module1
    Sub Main()
    Dim length(,) As Integer = {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}}
    Dim sum As Integer = 0
    Dim i, j As Integer
    For i = 0 To 4
    For j = 0 To 1
    Console.WriteLine("length[{0},{1}]:{2}", i, j, length(i, j))
    Next j
    Next i
    Console.ReadLine()
    End Sub
    End Module

  • The output of the above program will be as follows:
  • Output

    length[0,0]: 0
    length[0,1]: 0
    length[1,0]: 1
    length[1,1]: 1
    length[2,0]: 2
    length[2,1]: 2
    length[3,0]: 3
    length[3,1]: 3
    length[4,0]: 4
    length[4,1]: 4

Jagged Array


  • An array of array is known as Jagged Array.
  • One can declare a jagged array as follows:
  • Dim length As Integer()()= New Integer(3)(){}

  • Here is an example of Jagged array:
  • Example

    Module Module1
    Sub Main()
    Dim length()() As Integer = New Integer(3)() {}
    length(0) = New Integer() {0, 0}
    length(1) = New Integer() {1, 1}
    length(2) = New Integer() {2, 2}
    Dim i, j As Integer
    For i = 0 To 2
    For j = 0 To 1
    Console.WriteLine("length[{0}][{1}]:{2}", i, j, length(i)(j))
    Next j
    Next i
    Console.ReadLine()
    End Sub
    End Module

  • The output of the above program will be as follows:
  • Output

    length[0][0]: 0
    length[0][1]: 0
    length[1][0]: 1
    length[1][1]: 1
    length[2][0]: 2
    length[2][1]: 2

Dynamic Array


  • Dynamic Array is a type of array that can be dimensioned and re-dimensioned dynamically by using ReDim statement.
  • You can declare a dynamic array as follows:
  • ReDim Preserve length(10)

  • Here is an example of Dynamic array:
  • Example

    Module Module1
    Sub Main()
    Dim a() As Integer
    ReDim a(2)
    a(0) = 10
    a(1) = 20
    Console.WriteLine("Array after Redim 1")
    For j = 0 To a.GetUpperBound(0)
    Console.WriteLine(a(j))
    Next
    ReDim Preserve a(3)
    Console.WriteLine("Array after Redim 2")
    For j = 0 To a.GetUpperBound(0)
    Console.WriteLine(a(j))
    Next
    Console.ReadLine()
    End Sub
    End Module

  • The output of the above program will be as follows:
  • Output

    Array after Redim 1
    10
    20
    0
    Array after Redim 2
    10
    20
    0
    0