VB Array


An array stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.


Declare Array

DimintData(20)	  ' an array of 21 elements
Dim strinData(10) As String	' an array of 11 strings
Dimdcarray(10, 20) As Integer	'a two dimensional array of integers
Dim ranges(10, 100)	 'a two dimensional array

Initialize the array elements

Dim intData() As Integer = {02, 04, 08, 10, 12, 14}
Dim  member_name() As String	 = {"Sachin","Ashwani","Soun","Monu","Deepak","Amit"}

Example

Module arraydc
   Sub Main()
      Dim n(20) As Integer  ' n is an array of 21 integers 
      Dim i, j As Integer
      ' initialize elements of array n 
      For i = 0 To 10
          n(i) = i + 100 ' set element at location i to i + 100 
      Next i
      ' output each array element's value 
      For j = 0 To 10
          Console.WriteLine("Element({0}) = {1}", j, n(j))
      Next j
      Console.ReadKey()
   End Sub
End Module
Output
Element(0) = 100
Element(1) = 101
Element(2) = 102
Element(3) = 103
Element(4) = 104
Element(5) = 105
Element(6) = 106
Element(7) = 107
Element(8) = 108
Element(9) = 109
Element(10) = 110

Share this article on