Software applications often work with sets of
As an example, imagine that you were developing a zoo simulation application. You might include an array named
that includes each animal
Visual Basic identifies array elements by a
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
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
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
Visual Basic arrays support more than one
(or "rank"). The dimensions
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.
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
MsgBox("The board is " & _ (ticTacToeBoard.GetUpperBound(0) + 1) & _ " by " & (ticTacToeBoard.GetUpperBound(1) + 1))
Expert One-on-One Visual Basic 2005 Design and Development
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (O'Reilly))
Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
Beginning VB 2005 Databases: From Novice to Professional (Beginning: From Novice to Professional)