0% found this document useful (0 votes)
25 views

Lec 8

The document provides information on writing event procedures in Visual Basic by double clicking objects and outlines the basic syntax. It also discusses different data types in Visual Basic including numeric, non-numeric, and variant data types. Finally, it covers key concepts like declaring variables, constants, operators, and assigning values.

Uploaded by

JS Jayalath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Lec 8

The document provides information on writing event procedures in Visual Basic by double clicking objects and outlines the basic syntax. It also discusses different data types in Visual Basic including numeric, non-numeric, and variant data types. Finally, it covers key concepts like declaring variables, constants, operators, and assigning values.

Uploaded by

JS Jayalath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Writing the Code

Writing the Code


To start writing an event procedure, you need to
double-click on object. For example, if you want
to write an event procedure when a user clicks a
command button, you double-click on the
command button and an event procedure will
appear .It takes the following format:
Private Sub Command1_Click
(Key in your program code here)
End Sub
Writing the Code(Cont…)
Each control or object in VB can usually run
many kinds of events or procedures; these
events are listed in the dropdown list in the
code window that is displayed when you
double-click on an object and click on the
procedures’ box
The syntax to set the property of an object or
to pass certain value to it is :
Object.Property
Ex; Form1.Show -show the form with the name
Form1.
Iabel1.Visible=true -label1 is set to be
visible. Text1.text=”VB” -assign the text VB
to the text box with the name Text1.
Text2.text=100 -pass a value of 100 to the
text box with the name text2,
Timer1.Enabled=False -disable the timer
with the name Timer1
Visual Basic Data Types
Visual Basic classifies the information into
two major data types, they are
 numeric data types
 non-numeric data types
Numeric Data Types
Numeric data types are types of data that
consist of numbers, which can be computed
mathematically with various standard
operators such as add, minus, multiply, divide
and more……
(+,-,*,/)
Numeric Data Types(Cont…)
Non-numeric Data Types
• Nonnumeric data types are data that cannot
be manipulated mathematically using
standard arithmetic operators.
• The non-numeric data comprises text or
string data types, the Date data types, the
Boolean data types that store only two values
(true or false), Object data type and Variant
data type ….
Non-numeric Data Types (Cont…)
Suffixes for Literals
Literals are values that you assign to data. In
some cases, we need to add a suffix behind a
literal so that VB can handle the calculation
more accurately.
Ex.
Private Sub Form_Load()
Form1.Show

memberName = "Turban, John."


TelNumber = "1800-900-888-777"
LastDay = #12/31/2000#
ExpTime = #12:00:00 AM#

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:

Valid Name Invalid Name


My_Car My.Car
ThisYear 1NewBoy

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

m = 10 'm is a variable, 10 is a constant


n = 30
sum = m + n

Print "The sum is " & sum

End Sub
 You can declare many variables in one line as follows

Private Sub Form_Load()


Form1.Show
Dim m As Integer, n as Integer, sum as Integer
m = 10 :
n = 30
sum = m + n
Print "The sum is " & sum
End Sub
Ex:
Dim password As String
Dim yourName As String
Dim firstnum As Integer
Dim secondnum As Integer
Dim total As Integer
Dim doDate As Date
 You may also combine them in one line ,
separating each variable with a comma, as
follows:
Dim password As String, yourName As String,
firstnum As Integer,.............
Note
• For string declaration, there are two possible
formats, one for the variable-length string and
another for the fixed-length string.
• For the variable-length string
Dim yourName As String
• For the fixed-length string
Dim VariableName As String * n,
where n defines the number of characters the
string can hold.
Ex: Dim yourName As String * 10
yourName can holds no more than 10
Characters.
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 more….
Ex:
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
Implicit declaration : The Variant Data
Type
• If you use a variable without declaring its type,
it is called a variant variable/ Implicit
declaration.
Ex:
Dim num
Or,
Dim num As Variant
if you do not declare a data type for a variable
then also it is of the Variant data type. So you
can use a variable in Visual Basic without
declaring it. The variant data type can store
numeric, date/time or string values. This is
called implicit declaration. That means, you
are declaring the variable implicitly. But it is
not recommended to use implicit declaration
and a good programmer will declare the
variables explicitly. Because, it can lead to
errors that may not be detected at run time.
Option Explicit statement
The statement 'Option Explicit' is written in
general declaration section that reports the
user of any undeclared variable showing an
error message. That means, if you forget to
declare any variable, an error message will be
shown reporting it.
Ex:
What is Option explicit in VB?

Option explicit means that all variables must


be declared before they can be used.
Constants

Constants are different from variables in the


sense that their values do not change during
the running of the program.
Constant is a fixed value that does not change
during the program execution. You can define
your own constant to use it in your program.
Declaring a Constant
The format to declare a constant is
Const Constant Name As Data Type = Value
Ex:
Const Pi As Single=3.142
Const Temp As Single=37
Const Score As Single=100
Private Sub cmdCalculate_Click()
Const pi = 3.141 'or Const pi As Double =3.141
Dim area As Double, r As Double
r=2
area = pi * r * r
Print area

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

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)
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 …..

You might also like