Lesson 6: Working with Variables


Custom Search

 

<Previous Lesson> <<Home>> < Next Lesson>

6.1 Assigning Values to Variables

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:

Variable=Expression

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:

 

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
MeanScore% = SumScores% / NumSubjects%
X=sqr (16)
TrimString=
 Ltrim (“  Visual Basic”, 4)
Num=Int(Rnd*6)+1

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:

Example 6.1

Private Sub Command1_Click()
Dim MyNumber As Integer
MyNumber = "Happy Birthday!"
Text1.Text = MyNumber
End Sub

When you run the program, type mismatch occurred, as shown in Figure 6.1

 
 

  

6.2 Using Operators in Visual Basic

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: Arithmetic Operators

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"

 

 

Example 6.2

In this example, 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. The output is shown in Figure 6.2:

Private Sub Command1_Click()

Dim FirstName As String

Dim LastName As String

Dim YourName As String

FirstName = Text1.Text

LastName = Text2.Text

YourName = FirstName + "  " + LastName

          Label1.Caption = YourName

End Sub

 

 

 

Figure 6.2

Example 6.3

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

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

 

 

 

Bookmark and Share

 

<Previous Lesson> <<Home>> < Next Lesson>

Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved |Contact: admin@vbtutor.net

[Privacy Policy]