I searched, but it seems I have not blogged about this issue before. Sigh!
The scenarioI ran into yet another case of incorrect implicit declaration of variables. Simple examples would be:
Dim userName1, userName2
As StringDim empNum1, empNum2, empNum3, count
As IntegerThe problem with the scenarioThere seems to be an assumption that the variable type at the end of the line works for all the variables, but it just isn't true. The variables
userName1,
empNum1,
empNum2 and
empNum3 are all created with type
Variant, which clearly isn't what was intended. The proper declaration would be
Dim userName1
As String, userName2
As StringDim empNum1
As Integer, empNum2
As Integer, empNum3
As Integer, count
As Integeror in longer form
Dim userName1
As StringDim userName2
As StringDim empNum1
As IntegerDim empNum2
As IntegerDim empNum3
As IntegerDim count
As IntegerThe obvious question about the problem with the scenarioBut why bother? Why not just leave them as Variant variables, which can hold String or Integer values anyway?
The answer to the obvious question about the problem with the scenarioMost of the time, this would work. You script is not likely to break outright. On the other hand, the performance is likely to be worse, because Variant cause overhead. Memory usage will also be higher.
And sometimes, things break. A subroutine that expects a String might not appreciate a Variant. An LSX that expects an Integer value may choke on a Variant. A comparison with an uninitialized Variant isn't going to work where an uninitialized String might. When these things happen, the signs won't be clear, the debugging may be costly, and the person who didn't recognize the problem when declaring the variables may have a devilishly difficult time understanding what is happening.
The recommendation in response to the answer to the obvious question about the problem with the scenarioAlways declare your variables explicitly!
Copyright © 2004 Genii Software Ltd.