Scope & Lifetime of Variables

Ø The scope of a variable defines which parts of your code are aware of its existence.

Ø When you declare a variable within a procedure, only code within that procedure can access or change the value of that variable; it has a scope that is local to that procedure.

Ø Sometimes, however, you need to use a variable with a broader scope, such as one whose value is available to all the procedures within the same module, or even to all the procedures in your entire application. Visual Basic allows you to specify the scope of a variable when you declare it.

Scoping Variables

Depending on how it is declared, a variable is scoped as either a procedure-level (local) or module-level variable.

Scope

Private

Public

Procedure-level

Variables are private to the procedure in which they appear.

Not applicable. You cannot declare public variables within a procedure.

Module-level

Variables are private to the module in which they appear.

Variables are available to all modules.

Variables Used Within a Procedure

Procedure-level variables are recognized only in the procedure in which they're declared. These are also known as local variables. You declare them with the Dim or Static keywords. For example:

Dim intTemp As Integer

or

Static intPermanent As Integer

Values in local variables declared with Static exist the entire time your application is running while variables declared with Dim exist only as long as the procedure is executing.

Variables Used Within a Module

By default, a module-level variable is available to all the procedures in that module, but not to code in other modules. You create module-level variables by declaring them with the Private keyword in the Declarations section at the top of the module. For example:

Private intTemp As Integer

At the module level, there is no difference between Private and Dim, but Private is preferred because it readily contrasts with Public and makes your code easier to understand.


Used by All Modules

To make a module-level variable available to other modules, use the Public keyword to declare the variable. The values in public variables are available to all procedures in your application. Like all module-level variables, public variables are declared in the Declarations section at the top of the module. For example:

Public intTemp As Integer

You can't declare public variables within a procedure, only within the Declarations section of a module.

Multiple Variables with the Same Name

If public variables in different modules share the same name, it’s possible to differentiate between them in code. For example, if there is a public Integer variable intX declared in both Form1 and in Module1, you can refer to them as Module1.intX and Form1.intX to get the correct values.

The following example will clear all the doubts

Public intX As Integer ' Declare Module1’s intX.

Sub Test()

' Set the value for the intX variable in Module1.

intX = 1

End Sub

The second variable, which has the same name, intX, is declared in the second standard module, Module2. Again, a procedure named Test sets its value:

Public intX As Integer ' Declare Module2’s intX.

Sub Test()

' Set the value for the intX variable in Module2.

intX = 2

End Sub

The third intX variable is declared in the form module. And again, a procedure named Test sets its value.

Public intX As Integer ' Declare the form’s intX variable.

Sub Test()

' Set the value for the intX variable in the form.

intX = 3

End Sub

Each of the three command buttons’ Click event procedures calls the appropriate Test procedure and uses MsgBox to display the values of the three variables.


Private Sub Command1_Click()

Module1.Test ' Calls Test in Module1.

MsgBox Module1.intX ' Displays Module1’s intX.

End Sub

Private Sub Command2_Click()

Module2.Test ' Calls Test in Module2.

MsgBox Module2.intX ' Displays Module2’s intX.

End Sub

Private Sub Command3_Click()

Test ' Calls Test in Form1.

MsgBox intX ' Displays Form1’s intX.

End Sub

Public vs. Local Variables

Ø You can also have a variable with the same name at a different scope.

Ø For example, you could have a public variable named Temp and then, within a procedure, declare a local variable named Temp.

Ø References to the name Temp within the procedure would access the local variable; references to Temp outside the procedure would access the public variable.

Ø The module-level variable can be accessed from within the procedure by qualifying the variable with the module name.

Public Temp As Integer

Sub Test()

Dim Temp As Integer

Temp = 2

MsgBox Form1.Temp ' Temp has a value of 2.

End Sub

Private Sub Form_Load()

Temp = 1

End Sub

Private Sub Command1_Click()

Test ' Temp has a value of 2.

End Sub

In general, when variables have the same name but different scope, the more local variable always shadows (that is, it is accessed in preference to) less local variables. So if you also had a module-level variable named Temp, it would shadow the public variable Temp within that module (and the local Temp would shadow the module-level Temp within that procedure).

No comments:

Post a Comment