Re: Static variables in ASP .NET/VB .NET

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"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

.



Relevant Pages

  • Re: question about assigning null to a reference object
    ... for local variables, then they are not the same. ... The following code will also fail to compile, ... it is best to not initialize it at all. ... them instinctively initialize all variables at their declaration. ...
    (comp.lang.java.help)
  • Re: already used in a child scope to denote something else
    ... The scope of a local variable declared in a local-variable-declaration ... is the block in which the declaration occurs. ... The scoping rules for local variables are designed to guarantee ... that the meaning of a name used in an expression context is always the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: simple example
    ... This is unrelated with static int x=123. ... So using x even before the declaration means that you are trying to use ... A local variable named 'x' cannot be declared in this scope because it would ... you still have two x local variables named the same way in ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: in defense of GC
    ... declaration of that variable. ... In languages with full closures, *any* local variable can outlive it's ... Local variables can only be captured by the lexical scoping of a nested ... "Some nested procedure" should be very much obviously ...
    (comp.lang.ada)