Arrays


Software applications often work with sets of related data, not just isolated data values. Visual Basic includes two primary ways of working with such sets of data: collections (discussed in Chapter 16, "Generics") and arrays. An array assigns a numeric position to each item included in the set, starting with zero and ending with one less than the number of items included. An array of five items has elements numbering from 0 to 4.

As an example, imagine that you were developing a zoo simulation application. You might include an array named animals that includes each animal name in your zoo:

  • Animal #0: Aardvark

  • Animal #1: Baboon

  • Animal #2: Chimpanzee

  • Animal #3: Donkey

  • ...and so on...

Visual Basic identifies array elements by a parenthesized number after the array name. For our animals, a simple assignment puts the String name of each animal in an array element.

animal(0) = "Aardvark" animal(1) = "Baboon" animal(2) = "Chimpanzee" animal(3) = "Donkey" 


Using each array element is just as easy.

MsgBox("The first animal is: " & animal(0)) 


Each element of an array is not so different from a standalone variable. In fact, you could just consider the set of animals in the example code to be distinct variables: a variable named animal(0), another variable named animal(1), and so on. But they are better than ordinary variables because you can process them as a set. For instance, you can scan through each element using a For...Next loop. Consider an Integer array named eachItem with elements numbered from 0 to 2. The following code block adds the individual items of the array as if they were distinct variables.

Dim totalAmount As Integer totalAmount = eachItem(0) + eachItem(1) + eachItem(2) 


But because the items are in a numbered array, you can use a For...Next loop to scan through each element, one at a time.

Dim totalAmount As Integer = 0 For counter As Integer = 0 to 2    ' ----- Keep a running total of the items.    totalAmount += eachItem(counter) Next counter 


Before you assign values to array elements, or retrieve those elements, you must declare and size the array for your needs. The Dim statement creates an array just as it does ordinary variables; the ReDim statement resizes an array after it already exists.

Dim animal(0 To 25) As String ' 26-element array Dim moreAnimals() As String   ' An undefined String array ReDim moreAnimals(0 To 25)    ' Now it has elements 


Normally, the ReDim statement would wipe out any existing data stored in each array element. Adding the Preserve keyword retains all existing data.

ReDim Preserve moreAnimals(0 to 30)  ' Keeps elements 0 to 25 


Each element of the array is an independent object that can be assigned data as needed. In this example, each element is a String, but you can use any value type or reference type you wish in the array declaration. If you create an array of Object elements, you can mix and match the data in the array; element 0 need not contain the same type of data as element 1.

The array itself is also an independent objecta class instance that manages its set of contained elements. If you need to specify the entire array, and not just one of its elements (and there are times when you need to do this), use its name without any parentheses or positional values.

Multidimensional Arrays

Visual Basic arrays support more than one dimension (or "rank"). The dimensions indicate the number of independent ranges supported by the array. A one-dimensional array, like the animal array earlier, includes a single range. A two-dimensional array includes two comma-delimited ranges, forming a grid arrangement of elements, with separate ranges for rows and columns.

Dim ticTacToeBoard(0 To 2, 0 To 2) As Char ' 3 x 3 board 


An array can have up to 60 different dimensions, although there are usually better ways to organize data than breaking it out into that many dimensions.

Array Boundaries

The lower bound of any array dimension is always zero, as indicated by the "0 To x" clause when defining or redimensioning the array. You can actually leave the "0 To" part out of the statement, and just include the upper bound.

' ----- These two lines are equivalent. Dim animal(0 To 25) As String Dim animal(25) As String 


These two statements both create an array with 26 elements, numbered 0 through 25.

To determine the current lower or upper bound of an array dimension, use the LBound and UBound functions:

MsgBox("The board is " & (UBound(ticTacToeBoard, 1) + 1) & _    " by " & (UBound(ticTacToeBoard, 2) + 1)) 


If your array only includes a single dimension, you don't have to tell LBound or UBound which dimension you want to check.

MsgBox("The upper element is numbered " & UBound(animal)) 


Each array also includes GetLowerBound and GetUpperBound methods that return the same results as LBound and UBound. However, the dimension number you pass to the GetLowerBound and GetUpperBound methods starts from zero, while LBound and UBound dimension values start the counting at 1.

MsgBox("The board is " & _    (ticTacToeBoard.GetUpperBound(0) + 1) & _    " by " & (ticTacToeBoard.GetUpperBound(1) + 1)) 





Start-to-Finish Visual Basic 2005. Learn Visual Basic 2005 as You Design and Develop a Complete Application
Start-to-Finish Visual Basic 2005: Learn Visual Basic 2005 as You Design and Develop a Complete Application
ISBN: 0321398009
EAN: 2147483647
Year: 2006
Pages: 247
Authors: Tim Patrick

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net