Declaring Local Variables
In the What is a Variable? section, we demonstrated declaring the myMessage variable using the
Dim reserved word.
Here is the example that we used:
Dim myMessage As String
The meaning of the 4 pieces of information:
Dim - This reserved word sets aside memory in the computer for the "myMessage" variable.
myMessage - This is the actual variable. You can name your variable whatever you want, as long as it is not a reserved word (see the Reserved Words section for more information).
As - This reserved word tells Visual Basic to create the variable of the data type that follows.
String - This reserved word is the specific data type to create the variable as.
--------------------------------------------------------------------------------
In our example, the Dim reserved word was used to declare a local variable. A local variable is a variable that is declared within a Sub Procedure, Function or Property. Because it local, the variable can only be used within the Sub Procedure, Function or Property that it is declared in.
Here's an example, where we declare the myMessage variable within the Button1_Click Sub Procedure, store the text "Hello World" in it, and display the text in a message box.
Note: You can create a Click Sub Procedure for a button by adding a button to your form, and double-clicking on it
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Dim myMessage As String
myMessage = "Hello World"
MsgBox(myMessage)
End Sub
Because the myMessage variable is local to the Button1_Click Sub Procedure, it cannot be accessed from outside of that Sub Procedure.
--------------------------------------------------------------------------------
Now, if we create second Button Click procedure, and try to use the myMessage variable from above, Visual Basic will produce the following error: Name 'myMessage' is not declared.
Before we can run the program, we must fix the error by either declaring the myMessage variable, or removing our line of code. Here is the corrected code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button2.Click
Dim myMessage As String
myMessage = "Hello World"
MsgBox(myMessage)
End Sub
--------------------------------------------------------------------------------
Declaring Member Variables
Member variables are variables that are declared at module level, instead of within a Sub Procedure, Function or Property.
By declaring variables at a module level, variables can be shared between Sub Procedures, Function and Properties, without having to be declared locally. Member variables have greater accessibility than local variables.
Here's the example from above, but this time, the myMessage variable is a member variable, declared at module level, instead of locally within either of the Sub Procedures.
Dim myMessage As String = "Hello World"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
MsgBox(myMessage)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button2.Click
MsgBox(myMessage)
End Sub
No comments:
Post a Comment