< Day Day Up > |
Understanding the Array's IndexAt 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.
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 BaseBy default, the lowest bound value is always 0 unless one of the following conditions apply:
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.
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 > |