Hidden Variant variables

Hidden Variant variables

Variants are the default type of variables that are declared without an As clause, as in:

Dim name   ' this is a variant

or are not declared at all (which is only possible if the current module does not include any Option Explicit statement). If you are recycling programs written in Visual basic 1.0 or 2.0, or even QuickBasic, you might wish to include a DefSng A-Z at the very beginning of your modules, to make Single the default type as it was in those versions of the language. But the best approach is by far is using an explicit As clause for each declared variable.

A special case of this rule of thumb occurs when declaring more variables in the same line. Many developers, especially those that have programmed in C previously, are convinced that this line creates two Integer variables:

Dim x, y As Integer

while it actually makes x a variant variable. Since variant variables are generally much slower than Integer ones, you are going to have a performance hit if you do not pay enough attention to these details.


Share the Post: