Wednesday, April 22, 2009

Arrays in VB.NET

Array is a variable that store more than one value of same data type.Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the base class for all arrays in the common language runtime.

There is mainly three different kind of array
1) One dimensional array
2) Multi Dimensional array
Example for one dimensional array
Dim array1(4) as Integer
It holds five value with integer type.
array1(0)=1
array1(1)=2
array1(2)=3
array1(3)=4
array1(4)=5

Apart form the basic data type like integer, string ect… it also holds object like color
Example
Dim col(1) As Color
col(0) = Colors.Brown
col(1) = Colors.DarkGray


Reinitializing Array
Before we are initlize the array of specific type but later if we want to change its size we can alter using ReDim
Dim TestArray(5) As Integer
ReDim TestArray(15)


One drawback is once we reinitialize the array size we lose all the value in the array because every time it allocate new memory space.If you want to have the old data as before reinitialize then we have to use the Preserve keyword as below.
Dim TestArray() As Integer = {1, 3, 5}
ReDim Preserve TestArray(25)


GetLowerBound() and GetUpperBound() is used to get the first and last index of the array else we can use LBound(TestArray) and UBound(TestArray)
TestArray.GetLowerBound(0)
TestArray.GetUpperBound(0)


Multidimensional Array
To store more than one element of same or different data type in an array we are going to Multidimensional array. For example if we want to store Student name and his total in an array we can go for multidimensional array. Multi dimension means it may be two dimension or three dimension etc...

Some of example for multidimensional
Dim numbers(,) As Integer = {{1, 2}, {3, 4}, {5, 6}}Dim silings(,) As String = {{"Mike", "Amy"}, {"Mary", "Albert"}}

Console.WriteLine(number(0,1)) '20
Console.WriteLine(number(1,1)) 'Albert


Some more Examples

Public Class Customer
Public Id As Integer
Public Name As String
End Class


Now when you initialize this object in conventional VB.NET, this could be your approach,
Dim cust As New Customer()

With cust
.Id = 1
.Name = "VB.NET"
End With


Now in VB.NET 9.0 we do things in little differently,

Dim cust = New Customer() With {.Id = 2, .Name = "Kabilan"}

Also for array initialization in VB.NET we go for,

Dim objCusts(1) As Customer
objCusts(0) = New Customer() With {.Id = 3, .Name = "Kamal"}
objCusts(1) = New Customer() With {.Id = 4, .Name = "Ashok}
(or)
Dim objCusts() As Customer = { _
New Customer() With {.Id = 3, .Name = "Kamal"}, _
New Customer() With {.Id = 4, .Name = "Ashok"}}
}

Referrence Links
1) http://weblogs.asp.net/eporter/archive/2003/08/07/22943.aspx
2)http://msdn.microsoft.com/en-us/library/2yd9wwz4(VS.71).aspx
3)http://msdn.microsoft.com/en-us/library/system.array_members(VS.80).aspx
4)http://msdn.microsoft.com/en-us/library/0a7fscd0(VS.71).aspx
5) http://msdn.microsoft.com/en-us/library/2s05feca(VS.71).aspx
6)http://msdn.microsoft.com/en-us/library/szasx730(VS.71).aspx