Scope of a Variable

As you expand your knowledge of programming in Visual Basic, the scope of a variable is something that you should understand fully. A variable scope may be defined as the area of a program in which a variable is visible, and how long that variable is accessible for. There are 4 kinds of scopes for variables, let's take a look at each if them.

Block Level Scope

If you declare a variable within a block, you can use it only within that block. In the following example, the scope of a variable 'I' is the block between For and Next. FOR I = 1 to 10
  Msgbox "The number is " & I
  Msgbox "The square of the number is " & I * I
  Msgbox "The cube of the number is " & I * I * I
NEXT I
Note here that even if the scope of a variable is limited to a block, its lifetime is still that of the entire procedure. If you enter the block more than once during the procedure, each block variable retains its previous value. To avoid unexpected results in such a case, it is wise to initialize block variables at the beginning of the block.

Procedure Level Scope

These are also called local variables. These variables are accessible only in the subroutine in which they are declared. As a good practice, local variables are declared at the beginning of the subroutine. By doing this, all the local variables declared in the subroutine are in one place; i.e. at the start of the subroutine.

However, in Visual Basic you can declare the variables anywhere in the subroutine. The declaration is made by using the 'Dim' statement inside the sub and then declaring the variable name. For example: Sub Marks()
  Dim MarksinEnglish as Integer, MarksinMaths as Integer
  Dim MarksinHistory as Integer, TotalMarks as Integer
  MarksinEnglish = 70
  MarksinMaths = 85
  MarksinHistory = 65
  TotalMarks = MarksinEnglish + MarksinMaths + MarksinHistory
End Sub

Module Level Scope

A module level variable is available throughout all the subroutines of the module in which it is declared. These variables are declared (Dim) outside the subroutines at the very top of the module. Let's see an example.Dim MarksinEnglish as Integer, MarksinMaths as Integer
Dim MarksinHistory as Integer, TotalMarks as Integer

Sub Marks()
  MarksinEnglish = 70
  MarksinMaths = 85
  MarksinHistory = 65
  TotalMarks = MarksinEnglish + MarksinMaths + MarksinHistory
End Sub

Sub DisplayMarks()
  Msgbox "The Sum of all the marks is " & TotalMarks
End Sub
If you notice above, the DisplayMarks sub uses the TotalMarks variable that gets its value in the earlier subroutine. This is because all the declarations are made prior to the beginning of the subroutine; i.e. at the Module level. The scope of a variable here is across all subroutines within the module.

Global Level Scope

Global variables are available throughout the project; i.e. across all modules, subroutines and blocks. Global variables are defined by using 'Public' instead of using 'Dim'. They have to be declared outside the subroutine just like Module level variables. For example:Public UserName as String

What scope should you use?

When a variable is created, the value it stores is only available for a certain part of the code execution. Memory is set aside to store that piece of information and then freed when it is no longer needed.

When all variables are always available (global variables), memory is wasted because it is set aside well before or after it's needed. Therefore, it is a good programming practice to make the scope of a variable as narrow as possible (block level scope is the narrowest). This helps conserve memory and minimizes the chances of your code erroneously referring to the wrong variable.

Back to Visual Basic for Beginners