MIC 110

Declaring Module-level and Global Varaibles

Items in this page: Module-level, Global Varaibles, and Access a Variable in another Module

Module-level Variables

Module-level variables are accessible from any part of a form. Unlike local variables that are declared inside of a procedure or function and recognized only within that procedure or function, you can get and assign values to the module-level variables from anywhere in the form/module.   (To use variables for an entire program (passing values among forms), you may create a global variable. Here is how.)

Another difference of local and module-level variables is the time of initialization. When the form is loaded, both module-level and local variables get initialized to be zero or blank depending on their data types. However, the local variables also get reinitialized each time the procedure is called. So, as shown in the following example, each time when the cmdDisplay_Click procedure is activated, intNum is set to be 0. This could be a problem when you want to save the value of intNum for the next time. The solution is declaring and use the intNum as a module-level variable which will hold the value throughout the runtime of the form.

Here is an example of local variable that is only accessible within the procedure. The intNum in the cmdDisplay_Click procedure is recognized only in the procedure. Therefore, if you try to call in cmdLastNumber_Click procedure, you cannot access it.



In order to overcome the limitation, you can declare the intNum variable on the module-level. To do so, Dim intNum at the top of the program codes. Here is how:



One thing you need to be very careful about using a module-level variable is that because it can be accessed easily, you need to make sure you don't accidentally alter the value.

One may ask what if you declare an identically named variables on a module-level and a procedure-level. Let's say, you declare intNum as a module-level variable and declared intNum inside of the cmdDisplay_Click procedure. If you alter the value of intNum from the cmdDisplay_Click procedure, only intNum will change while intNum will stay the same. However, if you change intNum in another procedure, then the value of intNum will change while intNum will remain the same.

*** The MSDN suggests to use the key word Private instead of Dim for module-level variables. The following is information from the MSDN:

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.


Variables 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

Note: You can't declare public variables within a procedure, only within the Declarations section of a module. For More Information For additional information about variables, see "Advanced Variable Topics."


Access a Variable in another Module/Form

To read or assign a value in a variable in another form, add the name of the form followed by a period before the name of the variable.

For example:    frmMain.intTemp = 100     will assign 100 to the intTemp in the frmMain form.

The basic idea is to specify the name of the form that the intTemp variable is declared.