Planet For Application Life Development Presents
MY IT World

Explore and uptodate your technology skills...

VB.Net - Arrays

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.

Creating Arrays in VB.Net

To declare an array in VB.Net, you use the Dim statement. For example,

Dim intData(30)	  ' an array of 31 elements
Dim strData(20) As String	' an array of 21 strings
Dim twoDarray(10, 20) As Integer	'a two dimensional array of integers
Dim ranges(10, 100)	 'a two dimensional array

You can also initialize the array elements while declaring the array. For example,

Dim intData() As Integer = {12, 16, 20, 24, 28, 32}
Dim names() As String = {"Karthik", "Sandhya", _
"Shivangi", "Ashwitha", "Somnath"}
Dim miscData() As Object = {"Hello World", 12d, 16ui, "A"c}

The elements in an array can be stored and accessed by using the index of the array. The following program demonstrates this:

Module arrayApl
   Sub Main()
      Dim n(10) As Integer  ' n is an array of 11 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

When the above code is compiled and executed, it produces following result:

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

Properties of the Array Class

The following table provides some of the most commonly used properties of the Array class:

S.NProperty Name & Description
1IsFixedSize
Gets a value indicating whether the Array has a fixed size.
2IsReadOnly
Gets a value indicating whether the Array is read-only.
3Length
Gets a 32-bit integer that represents the total number of elements in all the dimensions of the Array.
4LongLength
Gets a 64-bit integer that represents the total number of elements in all the dimensions of the Array.
5Rank
Gets the rank (number of dimensions) of the Array.

Methods of the Array Class

The following table provides some of the most commonly used methods of the Array class:

S.NMethod Name & Description
1Public Shared Sub Clear ( array As Array, index As Integer, length As Integer )
Sets a range of elements in the Array to zero, to false, or to null, depending on the element type.
2Public Shared Sub Copy ( sourceArray As Array, destinationArray As Array, length As Integer )
Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. The length is specified as a 32-bit integer.
3Public Sub CopyTo ( array As Array, index As Integer )
Copies all the elements of the current one-dimensional Array to the specified one-dimensional Array starting at the specified destination Array index. The index is specified as a 32-bit integer.
4Public Function GetLength ( dimension As Integer ) As Integer
Gets a 32-bit integer that represents the number of elements in the specified dimension of the Array.
5Public Function GetLongLength ( dimension As Integer ) As Long
Gets a 64-bit integer that represents the number of elements in the specified dimension of the Array.
6Public Function GetLowerBound ( dimension As Integer ) As Integer
Gets the lower bound of the specified dimension in the Array.
7Public Function GetType As Type
Gets the Type of the current instance. (Inherited from Object.)
8Public Function GetUpperBound ( dimension As Integer ) As Integer
Gets the upper bound of the specified dimension in the Array.
9Public Function GetValue ( index As Integer ) As Object
Gets the value at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer.
10Public Shared Function IndexOf ( array As Array, value As Object ) As Integer
Searches for the specified object and returns the index of the first occurrence within the entire one-dimensional Array.
11Public Shared Sub Reverse ( array As Array )
Reverses the sequence of the elements in the entire one-dimensional Array.
12Public Sub SetValue ( value As Object, index As Integer )
Sets a value to the element at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer.
13Public Shared Sub Sort ( array As Array )
Sorts the elements in an entire one-dimensional Array using the IComparable implementation of each element of the Array.
14Public Overridable Function ToString As String
eturns a string that represents the current object. (Inherited from Object.)