Position of declaration statements

  • Thread starter Thread starter Peter Merwood
  • Start date Start date
P

Peter Merwood

Hi

I've been coding in VB for a good number of years and have recently made the
switch to VB.NET. When coding I put all my declaration/Dim statements at
the beginning of the Sub or Function before any "real" code. VB.NET lets
you scatter declaration statements throughout a Sub or Function and it seems
even have conditional declaration statements, ie;

If x = "something" then
dim y as Integer
dim z as Integer

<some code>

End if

Are there any performance advantages/disadvantages to be gained/lost by only
declaring variables immediately before you're going to use them as shown in
the above example? I've looked through the MSDN and .NET documentation for
any performance tips like this. Can someone point me to a good resource
that discusses these types of issues?

Thanks.

Peter
 
Peter Merwood said:
I've been coding in VB for a good number of years and have recently
made the switch to VB.NET. When coding I put all my declaration/Dim
statements at the beginning of the Sub or Function before any "real"
code. VB.NET lets you scatter declaration statements throughout a
Sub or Function and it seems even have conditional declaration
statements, ie;

If x = "something" then
dim y as Integer
dim z as Integer

<some code>

End if

Are there any performance advantages/disadvantages to be gained/lost
by only declaring variables immediately before you're going to use
them as shown in the above example? I've looked through the MSDN and
.NET documentation for any performance tips like this. Can someone
point me to a good resource that discusses these types of issues?


Variables at block scope are just like those declared at the start of the
procedure. The only difference is that they can be accessed only in the same
block, and consequently there can be variables with the same name in the
same procedure but in different blocks.

Whenever you declare something like this

for i= 1 to 100
dim y as New TheClass
next

the New statement is executed 100 times.
 
Armin

OK - so what you're saying is that it doesn't really matter where your
declaration statements are positioned as long as you're aware of the scope
rules and also take account of what you're initialising, (ie; your "New"
example).

Have I got that right?

Peter,
 
* "Peter Merwood said:
you scatter declaration statements throughout a Sub or Function and it seems
even have conditional declaration statements, ie;

If x = "something" then
dim y as Integer
dim z as Integer

<some code>

End if

Are there any performance advantages/disadvantages to be gained/lost by only
declaring variables immediately before you're going to use them as shown in
the above example? I've looked through the MSDN and .NET documentation for
any performance tips like this. Can someone point me to a good resource
that discusses these types of issues?

Variables declared in a block will be created when the procedure is
entered. You can see that when you use "ildasm.exe". Nevertheless,
these variables cannot be used outside the block they are declared in.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Improve your quoting style:
<http://learn.to/quote>
<http://www.plig.net/nnq/nquote.html>
 
Peter Merwood said:
OK - so what you're saying is that it doesn't really matter where
your declaration statements are positioned as long as you're aware of
the scope rules and also take account of what you're initialising,
(ie; your "New" example).

Have I got that right?

Yes, I think it can be expressed this way. :-) But I'd wait for statments
from other people,too. ;-) Personally I prefer the smallest scope possible.
 
Herfried

OK that clears it up. It seems it doesn't matter where you're declaring
your local variables (within a Sub or Function) as they're going to be
created when the Sub or Function is entered.

Thanks for clarifying this.

Peter,
 
Back
Top