Array Programming in VB.Net

by Ruben 4/10/2008 8:41:00 PM

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

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

7/3/2008 6:41:22 PM

Recieved Updates



Enter your email address:

Delivered by FeedBurner

About the author

Name of author RUBEN CORRAL
System Developer in outSourcing company for almost 8 years. I built this blogs just for fun, sharing idea's, contribute a piece of code, especially to newbie programmers.

E-mail me Send mail

Calendar

<<  July 2008  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

Disclaimer

The opinions expressed herein are my own personal point of view. Sample source codes are free to modify or enhance for your own satisfaction.

Sign in

All brand names, logos and trademarks in this site are property of their respective owners.