Lesson 6 : Working with Variables

6.1  Assigning Values to Variables

After declaring various variables using the Dim statements, we can assign values to those variables.
The general format 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 etc. The following are some examples:

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)
total = firstNumber + secondNumber+ThirdNumber
 

6.2 Operators in Visual Basic

In order 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.

Table 6.1


Operator Mathematical function Example
^ Exponential 2^4=16
* Multiplication 4*3=12
/ Division 12/4=3
Mod Modulus(return 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"

 

Example 6.1:

firstName=Text1.Text
secondName=Text2.Text
yourName=firstName+secondName
number1=val(Text3.Text)
number2=val(Text4.Text)
number3=num1*(num2^3)
number4=number3 Mod 2
number5=number4\number1
Total=number1+number2+number3+number4+number5
Average=Total/5
 

In lesson, we will see how do we use operators in writing the VB programs codes.
 
 

[Back to contents  page]