News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

Working with Variables

Started by magesh.p, Jul 24, 2008, 03:44 PM

Previous topic - Next topic

magesh.p

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   

Example 1:

        Dim firstName As String
        Dim secondName As String
        Dim yourName As String
   
         Private Sub Command1_Click()
        firstName = Text1.Text
        secondName = Text2.Text
        yourName = secondName + "  " + firstName
                   Label1.Caption = yourName
        End Sub


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.   

Example 2

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 above, three variables are declared as integer and two variables are declared as variant. Variant means the variable can hold any numeric data type. The program computes the total and average of the three numbers that are entered into three text boxes.
- An Proud Acumen -