Array Programming in VB.Net
Array programming is a data structure consisting of a group of elements that are accessed by indexing. In most programming languages each element has the same data type and the array occupies a contiguous area of storage.
Arrays in VB.NET inherit from the Array class in the System namespace. All arrays in VB as zero based, meaning the index of the first element is zero and they are numbered sequentially. You must specify the number of array elements by indicating the upper bound of the array. The upper bound is the number that specifies the index of last element of the array. Arrays are declared using Dim, ReDim, Static, Private, Public and Protected keywords. An array can have one dimension or more than one (multidimensional arrays). The dimensionality of an array refers to the number of subscripts used to identify an individual element.
The following code demonstrates arrays.
'single dimension array
Dim cars(3) As String
'populate the elements of cars variable
cars(0) = "Olds"
cars(1) = "Nissan"
cars(2) = "Toyota"
cars(3) = "Geo"
'In order to extend the array element used Redim Preserve
ReDim Preserve cars(4)
cars(4) = "Mercedes"
'MessageBox.Show(cars(0))
'MessageBox.Show(cars(4))
Dim counter As Integer
For counter = 0 To cars.Length
MessageBox.Show(cars(counter))
Next
'two dimensional array
Dim trucks(2, 2) As String
'populate the elements of trucks variable
trucks(0, 0) = "Toyota Tundra"
trucks(0, 1) = "Toyota Sequoia"
trucks(0, 2) = "Toyota 4Runner"
trucks(1, 0) = "Nissan Xtera"
trucks(1, 1) = "Nissan Pathfinder"
trucks(1, 2) = "Nissan Titan"
trucks(2, 0) = "Chevy Blazer"
trucks(2, 1) = "Chevy Suburban"
trucks(2, 2) = "Chevy Tahoe"
Dim i As Integer
Dim j As Integer
'outer loop
For i = 0 To trucks.GetUpperBound(0)
'inner loop
For j = 0 To trucks.GetUpperBound(1)
MessageBox.Show(trucks(i, j))
Next
Next
'tree dimensional array
Dim flights(2, 3, 4) As String
'populate the elements of flights variable
flights(0, 0, 0) = "Chicago - Dallas - 5:00am"
flights(0, 0, 1) = "Chicago - Dallas - 6:00am"
flights(0, 0, 2) = "Chicago - Dallas - 7:00am"
flights(0, 0, 3) = "Chicago - Dallas - 8:30am"
flights(0, 0, 4) = "Chicago - Dallas - 9:45am"
flights(0, 1, 0) = "Dulles - Chicago - 4:45am"
flights(0, 1, 1) = "Dulles - chicago - 5:30am"
'Total of 60 flights
FREE PDF BOOK DOWNLOAD