Write Your First Visual Basic Program
Insert two Command Buttons to your form (with
names Command1 and Command2),
and Add the following code to your program:
Dim gogo As Integer
gogo = 100
Private Sub Command2_Click()
Run the program, and click on the Command1 button.
code that found in the Command1_Click event will be executed.
variable will be declared, and it will store the value 10.
Now press on the Command2
In result, the MsgBox gogo Line will be
But instead of displaying the value of the gogo variable
It shows nothing, like if the gogo variable hasn't been declared at
The reason for all of this, is the
scope of the variable.
Every variable that been declared, "Exist" only in the
function that he was declared in.
What is Sub? (We will learn about functions
Sub is a Block of code that starts
with Sub and
ends with End Sub
Every event is a sub, because it begins
with Sub and
ends with End
Every line of code that found between
the Sub Command1_Click()
End Sub is belong to the sub
we've declared the gogo variable in the
Commad1_Click event, it's declared
only within the sub, and
not declared in the Command2_Click event.
So if you want that a
specific variable will be "exist" in
your whole program, no matter from which
sub you call it,
what can you do?
The Answer is in the next