Scope and Lifetime

The difference between lifetime and scope is quite simple.
Lifetime - Refers to how long or when the variable is valid (i.e. how long will it retain its value for).
Scope - Refers to where the variable can be accessed.
The difference between lifetime and scope is quite simple.
lifetime is how long the variable retains its value for.
The scope refers to where the variable can be used.


Lifetime vs Scope

The lifetime of the local variable "imylocal" extends from the moment ProcedureOne is entered to the moment ProcedureOne has finished.
However the local variable "imylocal" still exists when ProcedureTwo is called, it is just out of scope.

Public Sub ProcedureOne() 
Dim imylocal As Integer
   imylocal = 5
   Call ProcedureTwo()
   imylocal = 10
End Sub

Public Sub ProcedureTwo()
'do something
End Sub

In VB it was very important to set your Object references to Nothing when you have finished with the object.
In C# however it is sufficient to wait until the object references goes out of scope.
C# makes use of a very efficient garbage collection mechanism.
If you do want to set them to Nothing then the equivalent is null.


Variable Scope

The scope controls the lifetime and visibility of the variable. It is good programming practice to use the most restrictive scope possible for your variables.
You can also declare variables using the public and private keywords to explicitly indicate this scope.
Procedure / Local - variables and constants are local and are declared either using "Dim" or "Static" at the start of a procedure. A local (or procedure-level) variable or constant is declared within the procedure and is not visible outside of that procedure.
Module ("m" prefix) - variables and constants are declared as Private at the top of a module (always preceded by m). Module level variables are declared in the declaration section of a code module (standard, class or userform). These variables should be used if you want several procedures to have access to the same variable.
Global ("g" prefix) - variables are declared as Public at the top of a module level (always preceded by g). Public variables constants, procedures and functions are visible from any project that references the project where they are declared.


Every variable has a scope associated with it.
This scope refers to the area of code where the variables will be recognised.
Variables (and constants) have a defined scope within the program.
The scope for a variable can either be:
Procedure Level - Also known as a local variable.
Module Level Private - A private module level variable is visible to only the module it is declared in.
Module Level Public - A public module level variable is visible to all modules in the project.


Procedure Level

A local or procedure level variable is declared inside an individual procedure or function and is not visible outside that subroutine
Local variables can only be used in the procedure in which they are declared in.
When the procedure or function ends the variable is automatically removed and the memory is released.
You can use the Dim, Static or Private statement within a subroutine or function.
The most common way to declare a local variable is to use the Dim statement between the Sub and End Sub statements.
One of the great advantages of local variables is that we can use the same name in different subroutines without any conflicts.

Public Sub Procedure_Name() 
Dim slocalvariable As String

End Sub

One of the advantages of local variables is that you can use the same name in different subroutines without any conflict.



Module Level (Private)

A module level variable is declared in the declarations section of a code module (standard, class or userform).
Module level variables can have two flavours either Public or Private.
A private module level variable is visible to only the module it is declared in.
If you declare a module level variable with the Dim statement this is equivalent to using the Private statement.
Is this the same for constants ??
This often causes a lot of confusion and should definitely be avoided.

Private sModuleLevel As String 
Dim sModuleLevel As String

Public Sub Procedure_Name()

End Sub

Module Level (Public)

A public module level variable is visible to all modules in the project.
These are also know as project level or global variables
You must insert these statements at the very top of your modules, before any procedures or functions.
Project scope variables must aslo be declared in a standard code module and not is a userform or class module.

Public sProjectLevel As String 

Public Sub Procedure_Name()

End Sub


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext