PDA

View Full Version : Fully declaring each variable



tstav
03-24-2008, 02:13 AM
Good day everybody,

Dim intA, intB, intC as integer, lngL as long
Dim blnB, blnC as Boolean

The above is just an example of declaring various variables.
I see this particular way of declaring variables so often that I kind of started thinking it is correct. But it isn't.
In the above, even though the intention seems to be to declare intA and intB as type integer and blnB as type Boolean, this is not what happens. They all are of type Variant because no explicit type has been mentioned for them.

This is what Microsoft says about it:
' Multiple declarations on a single line. AnotherVar is of type Variant
' because its type is omitted.
Dim AnotherVar, Choice As Boolean, BirthDate As Date

Bob Phillips
03-24-2008, 02:17 AM
If people only declared one variable per line, such misconceptions might disappear.

tstav
03-24-2008, 02:32 AM
Absolutely, even though declaring one variable per line might create a pretty long list of declarations. The point is to be specific for each variable we declare.
Could be like this:
Dim intA as Integer, intB as Integer, intC as Integer, lngL as Long

Bob Phillips
03-24-2008, 02:49 AM
But a long list is not detrimental in any way, in fact it is useful. Group them alphabetically within data type and it is much simpler to scan the list than multi-variable, multi-type lines.