Lec 8
Lec 8
Print memberName
Print TelNumber
Print LastDay
Print ExpTime
End Sub
Managing Variables
Variables are like mail boxes in the post office.
The contents of the variables change every
now and then, just like the mail boxes. In term
of VB, variables are areas allocated by the
computer memory to hold data. Like the mail
boxes, each variable must be given a name. To
name a variable in Visual Basic, you have to
follow a set of rules.
What is the variable?
Variable is used to store value. The value of
the variable may vary during the program
execution.
Variable Names
The following are the rules when naming the
variables in Visual Basic
»It must be less than 255 characters
»No spacing is allowed
»It must not begin with a
number(variable name must begin
with an alphabet)
»The variable name must not contain
any special character like %,&,!,#,@ or
$.
Ex:
Long_Name_canbe_Used He&HisFather
Declaring Variables
In Visual Basic needs to declare the variables
before using them by assigning names and
data types. They are normally declared in the
general section of the codes' window using
the Dim statement.
Dim Variable Name As Data Type
Can declare a variable with the 'dim' keyword.
If you use a variable with declaring its type, it
is called a Explicit Declaration.
Ex:
Private Sub Form_Load()
Form1.Show
Dim m As Integer
Dim n As Integer
Dim sum As Integer
End Sub
You can declare many variables in one line as follows
End Sub
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.
Arithmetic Operators
Ex 1:
Dim firstName As String
Dim secondName As String
Dim yourName As String
(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)
Ex 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/3
Label1.Caption=Total
Label2.Caption=Average
End Sub
In the example 2 above, 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.
Thank You !
Next: Controlling Program Flow …..