After declaring various variables using the Dim statements, we can assign values to those variables. The syntax of an assignment is
Variable=Expression
The variable can be a declared variable or a control property value. The expression could be a mathematical expression, a number, a string, a Boolean value (true or false) and more.
The following are some examples variable assignment:
firstNumber=100 secondNumber=firstNumber-99 userName="John Lyan" userpass.Text = password Label1.Visible = True Command1.Visible = false Label4.Caption = textbox1.Text ThirdNumber = Val(usernum1.Text) X = (3.14159 / 180) * A
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.
Operator | Mathematical function | Example> |
---|---|---|
^ |
Exponential |
2^4=16 |
* |
Multiplication |
4*3=12, |
/ |
Division |
12/4=3 |
Mod |
Modulus (returns the remainder from an integer division) |
15 Mod 4=3 |
\ |
Integer Division(discards the decimal places) |
19\4=4 |
+ or & |
String concatenation |
"Visual"&"Basic"="Visual Basic" |
Private Sub Command1_Click() Dim firstName As String Dim secondName As String Dim yourName As String firstName = Text1.Text secondName = Text2.Text yourName = secondName +"" + firstName Label1.Caption = yourName End Sub
In Example 6.1, three variables are declared as string. For variables firstName and secondName will receive their data from the user’s input into textbox1 and textbox2, and the variable yourName will be assigned the data by combining the first two variables. Finally, yourName is displayed on Label1.
Dim number1, number2,number3 as Integer Dim total, average as variant Private sub Form_Click() number1=val(Text1.Text) number2=val(Text2.Text) number3= val(Text3.Text) Total=number1+number2+number3 Average=Total/5 Label1.Caption=Total Label2.Caption=Average End Sub
In the Example 6.2, 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.
This is a simple math drill program where the user enter two numbers and calculate its sum. The program will tell him whether the answer is right or wrong. To add some gist to the program, the user needs to enter the password before he or she can proceed.
Dim password As String Dim yourName As String Dim firstnum As Integer Dim secondnum As Integer Dim total As Integer Dim doDate As Date Private Sub Command1_Click() If userpass.Text = password Then Label2.Visible = True number1.Visible = True number2.Visible = True sum.Visible = True Label3.Visible = True Command3.Visible = True usernum1.Visible = True usernum2.Visible = True OK.Visible = True Label4.Visible = True Label4.Caption = textbox1.Text textbox1.Visible = False userpass.Visible = False username.Visible = False Label1.Visible = False Command1.Visible = False Else userpass.Text = "" userpass.SetFocus End If End Sub 3.' Private Sub Form_Load() password = "liewxun" End Sub Private Sub OK_Click() firstnum = usernum1.Text secondnum = usernum2.Text total = sum.Text If total = firstnum + secondnum And Val(sum.Text) <> 0 Then correct.Visible = True wrong.Visible = False Else correct.Visible = False wrong.Visible = True End If End Sub
Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy