Scope of Variables
(Continue) As you saw in the previous page, If you
declare variable in a sub, it exist only within the sub.
variable that will be exist in all subs, you have to declare it in
the "Declarations area" of your code. Choose "(General)" From the
components List in the code window (Figure 1).
Put the gogo declaration statement in the
"Declarations area". Simply write:
Dim gogo As
And delete the old statement that found in the Command1_Click
After you've done so, your code should look like
Dim gogo As Integer
Command1_Click() gogo = 100 End
Private Sub Command2_Click() MsgBox gogo End
Now the gogo variable is being declared
in the Declarations area of your code, and should be available from every
part of your code.
Lets check it out: Run the program. The gogo
variable is being declared immediately when the program is being
Press the Command2 Button. A message box with the number 0 is
popping. It is because the gogo variable has been declared, but we didn't
assign any value to it yet, so right now its value is the default value -
Press the Command1 Button. The value 100 is being assigned to the
Press the Command2 Button - a message box with the value 100 is popping.