Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What exactly is a global variable?

Status
Not open for further replies.

catalina36

Programmer
Aug 28, 2000
36
0
0
US
Simple point of clarification: What exactly is a global variable? Is it a variable that is declared as public in the Declarations section of ANY module or is it limited only to a variable declared as public in the standard/code module?

The book I’m currently reading is being rather ambiguous about this. I’d like to get my VB terminology straight.

TIA
 
"If you need to use a variable in more than one procedure, you can declare it in the Declarations Section at the start of a module using the Dim, Private, or Public key words. If you use Dim or Private, the variable is available only to the procedures within the module and is called a module-level variable. (Dim is the same as Private when coded in the Declarations section.) If you use the Public keyword, the variable is available to the procedures in all the modules in the application and is called a global variable."

From Murach's Visual Basic page 60.

Hope that helps!
 
Any variable declared as Public in ANY module in your project is "Gloabal", the only differnce is the way you would access that variable, for example.

Declared in a Code Module for myForm1
Public myVar

Declared in a Code Module for myForm2
Public myVar

Declared in a Standard Module
Public myVar


The variable declared in the standard Module could be accessed from anywhere in the project by just

myVar = 7

While the two variables in the Code Modules still can be accessed from any where in the project as long as you preface them with the Forms Name

myForm1.myVar = 7
myForm2.myVar = 7

All Modules are just Classes with a specific scope.

Hope this helps
Collin
 
Thanks for the responses. I know that this was a really basic question. I appreciate your help.

Jon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top