In Visual Basic 2013, data are stored either as a variable or as a constant. A variable is similar to a mailbox in the post office because of its contents changes from time to time, just like the mailbox. In VB2013, variables are areas allocated by the computer memory to store data.
In Visual Basic 2013, we have to assign a name to every variable. In order to name a variable, you have to follow a set of rules, as follows:
Some examples of valid and invalid variable names are illustrated in Table 9.1
|Valid Names||Invalid Name|
|Long_Name_Can_beUSE||LongName&Canbe&Use *& is not acceptable|
In VB2013, we declare a variable using the Dim statement. The syntax is as follows:
Dim VariableName As DataType.
Notice that you need to assign a name to the variable and also specify its data type. In addition, if you intend to declare more variables, you can declare them in separate lines or you can combine them in one line, separating each variable with a comma, as follows:
Dim VariableName1 As DataType1, VariableName2 As DataType2, VariableName3 As DataType3
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim password As String Dim yourName As String Dim firstnum As Integer Dim secondnum As Integer Dim total As Integer Dim doDate As Date End Sub
You may also combine the above statements in one line, separating each variable with a comma, as follows:
Dim password As String, yourName As String, firstnum As Integer,………….
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim YourMessage As String YourMessage = "Happy Birthday!" MsgBox(YourMessage) End Sub
When you run the program, a message box that shows the text "Happy Birthday!" will appear, as shown in Figure 9.1
After declaring variables using the Dim statements, we can then assign values to these variables. The syntax isVariable=Expression
The variable can be a declared variable or a control property value. The expressions include a mathematical expression, a number, a string, a Boolean value (true or false) and more, as illustrated in the following examples:
firstNumber=100 secondNumber=firstNumber-99 userName=”John Lyan” userpass.Text = password Label1.Visible = True Command1.Visible = false Label4.text = textbox1.Text ThirdNumber = Val(usernum1.Text) total = firstNumber + secondNumber+ThirdNumber MeanScore% = SumScores% / NumSubjects% X=sqr (16) TrimString= Ltrim (“ Visual Basic”, 4) Num=Int(Rnd*6)+1
In Visual Basic 2013, an error occurs when you try to assign a value to a variable of the incompatible data type. For example, if you have declared a variable as an integer but you assigned a string value to it, an error will occur, as shown in Example 9.4:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim YourMessage As Integer YourMessage = "Happy Birthday!" MsgBox(YourMessage) End Sub
When you run the program, the following error messages will appear in a dialog box, as shown in Figure 9.2
You can either break the program and continue to run the program.
Besides using the Dim keyword to declare the data, we may also use other keywords to declare the data. Three other keywords are private , static and public. The syntaxes are as shown below:
Private VariableName as Datatype Static VariableName as Datatype Public VariableName as Datatype
The aforementioned keywords indicate the scope of the declaration. First of all, the Private keyword declares a local variable which means it is local to a procedure or module. On the other hand, the Static keyword declares a variable that can be used multiple times, even after a procedure has been terminated. As a matter of fact, most variables created inside a procedure are discarded by Visual Basic when the procedure is finished. However, the Static keyword preserves the value of a variable even after the procedure is terminated. Lastly, the Public keyword declares a global variable, which means it can be used by all the procedures and modules of the whole program.
A Constant's value does not change during the running of the program.The syntax to declare a constant is
Const Constant Name As Data Type = Value
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Const Pi As Single = 3.142 Dim R As Single = 10 Dim AreaCircle As Single AreaCircle = Pi * R ^ 2 MsgBox("Area of circle with " & "radius" & R & "=" & AreaCircle) End Sub
Press F5 to run the program and clicking the button produces the following message: