Static Local Variables
Also known as Static method variables or method level static variables
These are not supported in C#.
The equivalent in C# would be a private static variable to the class
VB.Net static local variables are not thread safe
Static variables are not used very often but they can be very useful when used in the correct way.
Variables do have a lifetime.
Indicates that the local variable is preserved between calls.
A static variable can be thought of as a local variable with memory.
A static variable is a local variable whose lifetime is the lifetime of the entire module and not the procedure where it is declared.
In fact static variables retain their values as long as the code module is active. There does not have to be any code running all the time.
Therefore a static variable has the scope of a local variable but the lifetime of a module level variable.
Makes a variable persistent even after the procedure has completed.
The next execution of the routine can access the previous value.
It is the lifetime of a variable that determines its existence, the scope determines its visibility.
Private Module Level Variables
You can obtain the same effect using a private module level variable instead of a static variable.
It is considered better programming practice to use static variables as this uses a more restictive scope.
This helps to prevent accidental changes being made to the variable in other portions of the code.
Using "static variables" lets you retain the value of a variable that may go in and out of scope during execution, yet remain valid.
A static variable has the scope of a local variable although the lifetime of a module level variable.
Public Sub Procedure_One()
Public Function Function_Two() As Integer
Static iValue As Integer
iValue = iValue + 1
Function_Two = iValue
It is possible that a variable can go in and out of scope and yet remain valid during that time (ie keep its value).
Once the lifetime of the variable expires the value is lost.
It is the lifetime that determines the existence of a variable and it's the scope that determines its visibility.
Indicate that the variable is initialised the first time and is then preserved between function / procedure calls.
A static variable is a local variable whose life time is the lifetime of the entire module, not just the procedure
Static Procedures and Functions
To make all local variables in a procedure or function static, place the static keyword at the beginning of the procedure or function heading (eg Static Sub or Static Function).
Private Static Sub Procedure_One()
Dim slocalvariable As String
© 2020 Better Solutions Limited. All Rights Reserved. © 2020 Better Solutions Limited TopPrevNext