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.
"JohnFol" <Ou************@WibbleObbble.Com> wrote in message
news:9Y******************@newsfe3-win.ntli.net...
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?
"YGeek" <YG***@discussions.microsoft.com> wrote in message
news:0C**********************************@microsof t.com... 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