Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

Data Arrays

Now that you've mastered list boxes and combo boxes, you will have little trouble understanding data arrays. An array is nothing more than a list of variables. Regular nonarray variables, as opposed to arrays, have separate names such as the following:

curSales98     sngTaxRate     intCount    blnIsRecorded

Variables in an array all have the same name. Therefore, an array that holds a list of 10 division sales figures might be named curDivSales. Your program must be capable of distinguishing between an array's variables, and with the single name, this distinction might seem impossible. Nevertheless, as with list boxes, your program can distinguish between array variables by using a subscript. The subscript works just like an index value. The first value in the array would be subscripted as curDivSales(0) (subscripts start at 0 unless you use the Option Base 1 statement in a general module to start the array's subscripts at 1). The second value in the array would be curDivSales(1), and so on.

An array is a list of items with the same name and type.

Figure 10.6 illustrates how an array such as the 10-element curDivSales resides in memory.

10fig06.gif

Figure 10.6 Distinguishing array elements with subscripts.

To declare an array, you use Dim or Public just as you declare regular nonarray variables. In the declaration, specify the number of elements (in parentheses) that the array is to hold. The following Dim statement declares the 10-element Currency array named curDivSales:

Dim curDivSales(10) As Currency

Here's the great benefit that arrays give you over separate variable names: When you need to work with a group of variables, if you don't use an array, you must list each variable. Therefore, if you want to add all the divisions' sales figures and they are stored in separate nonarray variables, you would have to code something like this:

curTotal = curDivSales0 + curDivSales1 + curDivSales2 + _
curDivSales3 + curDivSales4 + curDivSales5 + _
curDivSales6 + curDivSales6 + curDivSales7 + _
curDivSales8 + curDivSales9

An array makes stepping through and totaling the data much simpler. Here is the code that uses a For...Next loop to add the items in an array:

curTotal = 0   ' Zero out the total
 ' Step through the elements
For intCtr = 0 To 9
curTotal = curTotal + curDivSales(intCtr)  ' Add elements
Next intCtr
 ' curTotal now holds the sum of all 10 values

With only 10 variables, the array doesn't seem to offer a lot of space advantages or coding shortcuts. But what if there were 1,000 variables that you needed to track and total? By making a simple change to the For...Next loop, you can easily add together all 1,000 elements, like this:

curTotal = 0   ' Zero out the total
 ' Step through the elements
For intCtr = 0 To 999
curTotal = curTotal + curDivSales(intCtr)  ' Add elements
Next intCtr
 ' curTotal now holds the sum of all 1,000 values

Suppose you need to ask the user for several values, such as the names of children in a class. By declaring a string array, a For...Next loop makes getting the names simple, as you can see here:

For intCtr = 1 To 10
   strChildName(intCtr) = InputBox("What is the next child's name?")
Next intCtr

					

Share ThisShare This

Informit Network