Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / General / April 2005

Tip: Looking for answers? Try searching our database.

Variable Declaration - Does Location Matter?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
YGeek - 27 Apr 2005 14:56 GMT
Is there any difference between declaring a variable at the top of a method
versus in the code of the method?  Is there a performance impact for either
choice?  What about if the method will return before the variable is used?  
I'm trying to get an idea of whether the .NET compilers for VB.NET and C#
will move all variable declaration to the beginning of a method or whether
they will allocate the memory as it is needed by the method to conserve
memory.

Example (VB.NET):
' Declaration at the top of a method.
Public Sub SampleSub()
 Dim sMessage As String
 ' ... Other code that might exit the method early ...
 sMessage = "Hello!"
End Sub

' Declaration in a method.
Public Sub SampleSub()
 ' ... Code that might exit the method early ...
 Dim sMessage As String = "Hello!"
End Sub
Michael D. Ober - 27 Apr 2005 18:03 GMT
In your case, they are syntactically identical.  However, in the following
code, they aren't:

Public Sub SampleSub()
   dim sMessage as String    ' sMessage is available throughout SampleSub

   for i as integer = 1 to 10
      sMessage = i.tostring    ' This works because i is local to the for
loop
   next i

   sMessage = "Final 'i' is " & i.toString        ' This will fail because
i isn't available outside the for loop
End Sub

Mike Ober.

> Is there any difference between declaring a variable at the top of a method
> versus in the code of the method?  Is there a performance impact for either
[quoted text clipped - 17 lines]
>   Dim sMessage As String = "Hello!"
> End Sub
AMercer - 27 Apr 2005 23:13 GMT
1.  In VB, I believe that Dim is an executable statement, whereas with C,
declarations have to be at the top of a block of code (ie at the top of a
{..} sequence).  A modest performance improvement should exist for a VB
return preceding a dim.  It could be significant if there are a lot of dim's
and/or big and complex initializers.

2.   I think it is a good idea to locate a dim statement in conjunction with
its usage/scope with a sub.  If a variable is used throughout a sub, then dim
it at the top of the sub.  If it is used in a small area of a relatively
large sub, then dim it near where it is used.  If you dim within an if-then
block (for example), you do limit the scope of the variable.  The following
sub will compile ok:
 Private Sub x()
   Dim Something as boolean
   If Something Then
     Dim b As integer
   End If
   ' b=0 ' under option strict, this will not compile because b is not
declared
 End Sub
Above, the scope of 'b' is the 'then' block.

3.  Continuing the above example (both Basic and C), if boolean SomeThing is
false, the 'then' block will not execute, and that includes dim'ing 'b'.  If
there are many dim statements with initializers, the performance change may
be significant.  

> Is there any difference between declaring a variable at the top of a method
> versus in the code of the method?  Is there a performance impact for either
[quoted text clipped - 17 lines]
>   Dim sMessage As String = "Hello!"
> End Sub
JohnFol - 28 Apr 2005 14:56 GMT
I took a different understanding of the question. It's not the declaration
location that is changing in the example, but where it is assigned the value
"Hello!"

Does this make any difference?

> Is there any difference between declaring a variable at the top of a
> method
[quoted text clipped - 19 lines]
>  Dim sMessage As String = "Hello!"
> End Sub
Scott M. - 29 Apr 2005 15:56 GMT
It's all about where the variable is declared, not where it is assigned.
The location that a variable is declared determines its "scope" or lifetime
or accesibility.

Here are ALL the VB.NET levels of scope:

The smallest scope there is in VB.NET is "Block Level Scope".  A "block" of
code is anything that can be written in a procedure that has a beginning and
an end.  For example, For...Next, If...End If, Try...End Try, Select...End
Select are all examples of blocks of code.  Be careful though because a
block may contain smaller blocks inside of it:

If foo then
   'this is a block
elseif foo then
   'this is a different block
else
   'this is a different block
end if

So, anything declared in a block is only accessible from within that block.
The variable pointer itself will stay in memory until the procedure ends.

Anything declared within a procedure, but not in a block with the word Dim
has "Procedure Level Scope" and is accessible from anywhere in the procedure
(including inside of blocks).  The variable pointer itself will stay in
memory until the procedure ends.

Anything declared within a procedure with the keyword Static will is
accessible as if it was declared with Dim (in or out of a block) but with
the difference that the variable's value will not be lost when the variable
falls out of scope.  If the program should re-enter the procedure, the
variable's last value will still be accesible.

Anything declared at the module level (not inside a procedure) with the
keyword Dim or Private is accessible from any procedure within the module.
The variable pointer itself will stay in memory until the module is removed
from memory.

Anything declared with the keyword Protected is accessible as if it was
declared Private at the module level with the exception that it is also
accessible from classes that derive from the class the the keyword was used
in.

Anything declared with Freind is accessible anywhere in the assembly that
the declaration appears in.

Anything declared with Public is accessible anywhere in the assembly as well
as outside the assembly.

Anything delcared as a Web Service is available from other machines via a
web reference.

Anything in a Web Service that is declared as a Web Method is available to
be called by the remote machine that has made an instance of the web service
class.

>I took a different understanding of the question. It's not the declaration
>location that is changing in the example, but where it is assigned the
[quoted text clipped - 27 lines]
>>  Dim sMessage As String = "Hello!"
>> End Sub

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.