After declaring various variables using the Dim keywords or other keywords, we need to assign values or information to those variables. Assigning a value to a variable means storing the value in that variable. The form of an assignment statement is as follows:
The variable can be a declared variable or a control's property value. The expression could be a mathematical expression, a number, a literal value, a string, a Boolean value (true or false) , a combination of other variables and constants, a function and more. The following are some examples:
A type mismatch error occurs when you try to assign a value to a variable of incompatible data type. For example, if you have declared a variable as an integer but you assigned a string value to it, a type mismatch error occurred, as shown in Example 6.1:
Private Sub Command1_Click()
Dim MyNumber As Integer
MyNumber = "Happy Birthday!"
Text1.Text = MyNumber
When you run the program, type mismatch occurred, as shown in Figure 6.1
To compute inputs from users and to generate results, we need to use various mathematical operators. In Visual Basic, except for + and -, the symbols for the operators are different from normal mathematical operators, as shown in Table 6.1.
FirstName = Text1.Text
LastName = Text2.Text
YourName = FirstName + " " + LastName
Label1.Caption = YourName
In this example, three variables are declared as integer and two variables are declared as variant. Variant means the variable can hold any data type. The program computes the total and average of the three numbers that are entered into three text boxes.
Private sub Form_Click
Dim number1, number2, number3 as Integer
Dim total, average as variant
Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved |Contact: firstname.lastname@example.org