Understanding Variable Scope and Duration


© Swapna Kamat

Understanding Variable Scope and Duration

Understanding Variable Scope and Duration There are two key attributes of all variables in Visual Basic that are essential to understanding their use:

  • Scope
    Scope defines the visibility of a variable. Variables can have scope ranging from global where any procedure in the application can access the variable to local to a single procedure.
  • Duration
    Duration defines the lifetime of a variable. Variables can exist for the life of an application or can be created and destroyed in a single procedure.

Variable Scope

The scope of a variable determines where you can access that variable in your code. If a variable is in scope you can read or set it's value. If it is out of scope you will not be able to access it.

There are three types of scope for variables in Visual Basic:

  • Global Scope
    Global variables are in scope anywhere in your application.
  • Module Scope
    Module level variables are in scope anywhere within the module where they are declared.
  • Local Scope
    Local variables are only in scope within the procedure where they are declared.

Global Scope

Global variables are available anywhere in your program. Any line of code in any procedure can read or write the value of the variable. While convenient, it is considered bad programming practice to overuse global variables and some programmers (myself included) make a considerable effort to avoid them entirely.

To create a global variable, declare it in the declarations section of a standard module using the Global or Public keyword.

Module Scope

Module level variables are available to any code within the module where they are declared. While using global variables is considered bad programming practice, using module level variables is not. Module level variables allow you to share data between procedures without exposing that data to every procedure in the application.

To create a module level variable, declare it in the declarations section of a module using either the Dim or Private keyword.

Local Scope

Local variables are only available to the procedure in which they are created. Local variables are the most restricted in scope - not even other procedures in the same module may read or modify local variables.

You create local variables by declaring them with the Dim or Static keyword within the body of a Sub, Function, or Property procedure.

Variations

There are a few variations from the basic scoping rules:
  • Public Variables in Class Modules
    Unlike public variables in standard modules, public variables in Class

Go To Page: 1 2


The copyright of the article Understanding Variable Scope and Duration in Learning Visual Basic is owned by . Permission to republish Understanding Variable Scope and Duration in print or online must be granted by the author in writing.

Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo