This lesson will cover scoping and lifetime of variables in procedures and modules.
What is scope?
Scope of a variable determines which part of the code can access the variable. A variable is declared in general declaration section of a module to make it available to all the procedures in the module.
There are two levels in which you can define variables:
- Procedure level
- Module Level
Example
The following block of code is a procedure.
Private Sub Command1_Click() Dim num As Integer num = 45 Print num End Sub
‘Command1_Click()’ is the procedure name. Here ‘num’ is a procedure-level variable. The value of the variable is destroyed when the procedure ends.
‘End Sub’ indicates the end of the event procedure.
What is a procedure?
Procedure is a block of code that is executed as a unit.
Types of procedure
There are three types of procedures.
- Sub procedure
- Function procedure
- property procedure
Sub procedures are of two types:
- General procedure
- Event procedure
Modules
Visual Basic uses modules. The three module types are:
- Form Modules
- Standard Modules
- Class Modules
Form Module
Combination of procedures in a form is referred to as form module.
Standard Module: (.bas extension)
Go to
Menu -> Project -> add module
to add a module to your application. This type of module is used for a common code to be executed in several forms or all forms.
QuickHint:
Form module is used for the execution of a common code repeatedly only in that form. But standard modules can be used for the repeated execution of a common code in all forms.
Class Module (.cls extension)
Class module is used for object oriented programming.
Here procedures and modules have been discussed very briefly. A complete chapter is dedicated on these topics.
Procedure Level Variable or Local Variable
Local variables are of two types – Dynamic local variable and Static local variable.
Dynamic local variable
A Dynamic local variable is declared inside a procedure using the dim keyword. Local variables are only available to the procedure in which they are declared. Other parts of the code are unaware of its existence. Values of the variables declared using the dim keyword in a procedure exist only within the procedure.
Example
Private Sub Command1_Click() Dim num As Integer 'This is a dynamic local variable End Sub
Static Variable
Static variable is a procedure level variable.
Syntax
static variable as [type]
Example
static m as integer
Static variables are declared inside a procedure and it retains its value even when a procedure ends but the scope is the procedure itself.
Module level variable
By default, a module level variable is accessible from all the procedures in the module but not from other modules. A module level variable is declared in the Declarations section, at the top of the module using the Dim or Private keyword. There is no difference between Dim and Private at the module level.
Example
'in the Declarations Section Private s As String
Public Variable or Global variable
To make a variable accessible to all the modules or throughout the application, declare it with the Public keyword in the Declarations section of a module. Then the value of the variable becomes available to all the procedures of your application.
QuickHint:
You cannot declare a public variable inside a procedure, public variables are declared only within the Declarations section of a module.
Example
'Form1 'in the form's declaration section Public n As Integer
'Form2 'To access from another form msgbox Form1.n
'To access a global variable declared in a .BAS module msgbox Module1.n
The concept of scope and lifetime of a variable is very important. When you will learn a bit more about vb coding, you will understand much in this respect.