Arrays and VariantsThis content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.
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 is not necessary to fill the array 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 might 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 is easy to sort. But if you're storing objects, you probably do not care about the sort order. Because a Collection or Dictionary object resizes itself automatically, you do not have to worry about keeping track of its size, as you do with an array.
Understanding Arrays | Creating Arrays | Assigning One Array to Another | Returning an Array from a Function | Passing an Array to a Procedure | Sorting Arrays | Using the Filter Function to Search String Arrays | Using a Binary Search Function to Search Numeric Arrays | Searching a Dictionary