Testing For and Coercing Subtypes

There are two built-in VBScript functions that allow you to check what the subtype is for Variant variable. These functions are VerType ( ) and TypeName ( ). These two functions do pretty much the same thing, but VarType ( ) returns a numeric representation of the subtype and TypeName ( ) returns a string representation.

VBScript also provides some functions that you can use to force (or ‘coerce’) the Variant to have a specific subtype. These conversion functions are especially useful when you need to pass data of a certain data type to a VB/COM object that expects data of a specific data type. This is also useful when you want to ensure that the value stored in a Variant variable is treated in a certain way. For example, the value 12 can be stored in a Variant variable with either a String subtype or one of the numeric subtypes. If you want to make sure that the number 12 is treated as a number, and not a string, you can use the CLng ( ) conversion function to make sure that the subtype is Long and not String.