Scope of a Variable


Scope of a variable determines which part of the code can access the variable.  A variable is declared in the general declaration section of a module to make it available to all the procedures in the module.

Concepts of procedures and modules are necessary before jumping on the scope part. So procedures and modules are clarified first.




What is a procedure? 

Procedure is a particular block of code. Examine the following example.
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.



   

Types of procedures 

There are three types of procedures - sub procedure, function procedure and property procedure 

Read the following lessons to learn more.




Sub Procedures are of two types

a)General procedure and 
b) Event procedure.

      

Module 

Visual Basic uses modules. Three kinds of modules are there - form modules, standard modules, and class modules.

i) Form module: The collection of procedures in a form is referred to as form module.

ii) Standard Module:  (.bas extension) 

The standard module has the .bas extension. Go to Menu ->Project ->add module to add a module to your application.


The following lesson can be helpful.

iii) 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. The static variables retain their values even when a procedure ends but their 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.

Note: You cannot declare a public variable inside a procedure. Public variables are declared only within the general 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