Microsoft Office 2000/Visual Basic Programmer's Guide |
A Variant variable can store an array. For example, the following code fragment assigns an array of type String to a Variant variable:
Dim astrItems(0 To 9) As String
Dim varItems As Variant
varItems = astrItems
When a static array is initialized, or when a dynamic array is redimensioned, each element is initialized according to its type. In other words, String type elements are initialized to zero-length strings, Integer and Long type elements are initialized to zero (0), and Variant type elements are initialized to Empty. The point is that in the preceding example, it's not necessary to fill the array in order to work with it. By simply declaring an array of ten elements as type String, we've created an array containing ten zero-length strings.
An array of type Variant can store any data type in any of its elements. For example, a Variant type array can have one element of type String, one element of type Long, and another of type Date. It can even store a Variant variable that contains another array.
A Variant type array can also store an array of objects. If you know that an array will store only objects, you can declare it as type Object rather than as type Variant. And if you know that an array of objects will contain only one type of object, you can declare the array as that object type.
Tip You may want to consider using a Collection or Dictionary object to store groups of objects in a single variable, rather than creating an array of objects. The advantage to using an array over a Collection or Dictionary object is that it's easy to sort. But if you're storing objects, you probably don't care about the sort order. Since a Collection or Dictionary object resizes itself automatically, you don't have to worry about keeping track of its size, as you do with an array.