Typeless Visual Basic

In Visual Basic 5, the question, “Is this variable of the correct type?” has become, “Does this variable reference an instance of a class that supports this interface?” Although we are dealing with Variants, there are similarities between the way you program with Variants and the way you program with classes. You can think of a Variant as a little class that contains just one property—its value. As is the case with a class, your access to that property is not direct—there is a layer of encapsulation. As a user of a class, you are not concerned with the implementation of the class so much as its behavior. Likewise, you can ignore the underlying implementation of a Variant and concern yourself with its behavior. In classes, the definition of the behavior is its interface. With Variants, we might think of the subtypes as various interfaces. To see whether the Variant supports them, we can use the set of Is… functions to check to see that the interface is supported.

For example, to see if a Variant is an array, we can use IsArray(). Good defensive programming should test with IsArray before using functions such as UBound on a Variant. If you get into this habit, you will find your code is more robust.