Write Your First Visual Basic Program
Scope of Variables
As you saw in the previous page,
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
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
gogo = 100
Private Sub Command2_Click()
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.
variable is being declared immediately when the
program is being
Press the Command2 Button.
A message box with the number 0 is
It is because the gogo variable has been declared, but
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.