Re: Static variables in ASP .NET/VB .NET
- From: "Mark Rae [MVP]" <mark@xxxxxxxxxxxxxxxxx>
- Date: Thu, 1 Nov 2007 11:09:40 -0000
"Rory Becker" <RoryBecker@xxxxxxxxxxxxxxxx> wrote in message news:b0ac48a0213778c9ea8d5ef038a0@xxxxxxxxxxxxxxxxxxxxxxx
In C# (unlike C++), static variables can't be declared inside methods
Fair enough but the subject of this post details that the OP was using VB.net which does allow such.
VB used Static as a keyword for this since a long way before VB.Net which is probably why Shared is used to mean Static in the other sense.
Apologies - you're quite correct. According to MSDN:
[Static] Specifies that one or more declared local variables are to remain in existence and retain their latest values after termination of the procedure in which they are declared.
Remarks
Normally, a local variable in a procedure ceases to exist as soon as the procedure terminates. A static variable remains in existence and retains its most recent value. The next time your code calls the procedure, the variable is not reinitialized, and it still holds the latest value you assigned to it. A static variable continues to exist for the lifetime of the class or module in which it is defined.
Rules
Declaration Context. You can use Static only on local variables. This means the declaration context for a Static variable must be a procedure or a block within a procedure, and it cannot be a source file, namespace, class, structure, or module.
You cannot use Static inside a structure procedure.
Combined Modifiers. You cannot specify Static together with ReadOnly, Shadows, or Shared in the same declaration.
Behavior
The behavior of any local variable depends on whether it is declared in a Shared procedure. If the procedure is Shared, all its local variables are automatically shared, including the Static variables. There is only one copy of such a variable for the entire application. You call a Shared procedure using the class name, not a variable pointing to an instance of the class.
If the procedure is not Shared, its local variables are instance variables, including the Static variables. There is an independent copy of each variable in each instance of the class. You call a nonshared procedure using a variable pointing to a specific instance of the class. Any variable in that instance is independent of a variable with the same name in another instance. Therefore, they can hold different values.
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
.
- References:
- Re: Static variables in ASP .NET/VB .NET
- From: Mark Rae [MVP]
- Re: Static variables in ASP .NET/VB .NET
- From: Rory Becker
- Re: Static variables in ASP .NET/VB .NET
- Prev by Date: Re: Static variables in ASP .NET/VB .NET
- Next by Date: Re: New browser window
- Previous by thread: Re: Static variables in ASP .NET/VB .NET
- Next by thread: Re: Debugger not working in copy of VS 2003 ASP.Net
- Index(es):
Relevant Pages
|