Declaring variables - best approach

hog

Senior Contributor
Joined
Mar 17, 2003
Messages
984
Location
UK
Could someone explain if there is any performance advantages between the two declaration snippets below or is it just a .exe file size issue?

Visual Basic:
Dim intX as Int32
Dim intY as Int32
Dim intZ as Int32

Visual Basic:
Dim intX, intY, intZ as Int32
 
I doubt there is a difference at all. The compiler will most probably optimize his execution time anyway.

I recommend the upper style, though.
Makes it easier to read, comment and edit.
 
I use the upper method for most things, because it allows you to initialize the variable, and use the 'New' keyword to initialize the object.
 
I would tend to use the upper method also, makes it easier to check data types of declared objects and generally looks cleaner to read.
 
Also, (not sure if this carried over to .NET but) I think in 6.0, the lower style would only declare the specific type to the last var in the list.
e.g.
Visual Basic:
Dim var1, var2, var3 As Integer
this would end up with var1 & var2 as variable type Variant and only var3 would actually be an Integer. As I'm typing this, I'm having doubts as to weather or not this was the case, however, I seem to recall this hapening to me once or twice before I figured it out. If I'm just way out in left field (as usual...) just ignor me. I don't get much sleep these days!
 
That is no longer the case under .NET, Mothra. Variables declared in a list do not default to Object; they are declared as is specified by the As <Type> keywords.
 
Good to know, thanks Derek. Although, I'll be using the one line per declaration also, if for nothing more than out of habit. And I do find it easier to read.
 
hog said:
Could someone explain if there is any performance advantages between the two declaration snippets below or is it just a .exe file size issue?

Visual Basic:
Dim intX as Int32
Dim intY as Int32
Dim intZ as Int32

Visual Basic:
Dim intX, intY, intZ as Int32

heh, this first way is easier to read, thes econd way takes up less space.. my mario game for instance, as about like 30 variables( i think) so i try to use the lower method
 
A good way to conserv space (is with regions):
Visual Basic:
#Region " Variables "
Dim blah As Blah
' ...
' ...
' ...
#End Region
Then you can simple expand and contract the region. :)
 
Back
Top