Understanding the Array s Index

 < Day Day Up > 

Understanding the Array's Index

At this point, you might be wondering how VBA and you differentiate between all the possible elements (values) in an array. Internally, VBA assigns an index to each element in the array. Simply put, the index is a type of identification value for each element.

The index values depend on the lower and upper bounds. For instance, the statement

 

 Dim arrInteger(1 To 4) As Integer 

accommodates four Integer values. These four values are stored in the four array elements, each identified by one of the four index values 1, 2, 3, and 4. The first element's index value is 1, the second element's index value is 2, and so on.

Table 7.1 lists several simple arrays that can also store up to four Integer values, but the index values would be different. As you can see, the numbering of the elements is determined by the upper and lower bounds in the array declaration.

Table 7.1. Array Examples

Array Declaration

Index Values

Dim arrInteger(0 To 3) As Integer

0, 1, 2, 3

Dim arrInteger(2 To 5) As Integer

2, 3, 4, 5

Dim arrInteger( 4 To 1) As Integer

4, 3, 2, 1

Dim arrInteger ( 3 to 0) As Integer

3, 2, 1, 0

Dim arrInteger (3) As Integer

0, 1, 2, 3


TIP

Occasionally, the index value relates to the stored value in some way, but don't confuse the two. The index is simply VBA's way of finding the value it identifies the position of the element in relation to the other elements. For example, the first element in an array of integers can contain the value 1, or 17, or 257, or any other integer value. Don't try to find a way to relate the index values to the actual stored values unless the relationship just naturally exists, which probably won't happen very often.


Using Option Base

By default, the lowest bound value is always 0 unless one of the following conditions apply:

  • You explicitly specify a different lower bound value.

  • The module includes an Option Base statement.

You've already seen the first condition at work, which leaves the Option Base statement. You enter this statement in a module's General Declarations section using the form

 

 Option Base 0 | 1 

This statement sets the default lowest bound for arrays within that module, and only that module. You can set the default bound to 0 or 1, not to any other value.

The general declarations section of a module is reviewed in "Introducing the VBA Modules," p. 21.


TIP

Although the Option Base 1 statement automatically sets the lower bound of all arrays within the module to 1, consider explicitly specifying the lower bound for each individual array instead. Doing so creates a more readable array statement. You'll also prevent those possible (and likely) errors after you forget that the default lower bound has been set to 1 by the Option Base statement.


     < Day Day Up > 


    Automating Microsoft Access with VBA
    Automating Microsoft Access with VBA
    ISBN: 0789732440
    EAN: 2147483647
    Year: 2003
    Pages: 186

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