Visual Basic.
NET
Week2
APIIT City Campus – 2008 Feb – Last Modified by
At the end of this lecture you should be able to:
1. Write a simple VB .Net programs
2. Use input and output statements
3. Become familiar with data types
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
To begin learning how to code in VB.NET, you
need to understand
• The types of data that you can work with
• How can store that data in variables and
• How you can convert one data type to another.
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
Variable
• A variable is a pointer to a block of memory that stores
information.
Data Type
• The information that is stored in a variable is of a
certain data type.
• This data type you work with could be a number, a
string or a date.
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
Use keyword Dim declare variables and data
types
A variable name can be any valid identifier
Variables of the same data type can be declared
in separate statements or can be declared in
one statement with each variable in the
declaration separated by a comma
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
Visual Basic structure Value range
type Storage size
Boolean 4 bytes True or False
Byte 1 byte 0 to 255 (unsigned)
Char 2 bytes 0 to 65535 (unsigned)
String 10 bytes + (2 * 0 to approximately two billion Unicode
string length) characters
Date 8 bytes January 1, 1 CE to December 31, 9999
Decimal data types
Decimal 12 bytes 28 places to the right of the decimal
Double 8 bytes 14 places to the right of the decimal
Single 4 bytes 7 places to the right of the decimal
Integer Data types
Integer 4 bytes -2,147,483,648 to 2,147,483,647
Long 8 bytes -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
Short 2 bytes -32,768 to 32,767
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
• To declare variables in a program.
Dim a as integer
Dim b as string
Dim c as single
Dim d as Double
Dim e as long
Dim num1, num2, num3 As Integer
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
Public Class Form1
Dim a As Integer
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = "Welcome to Visual Basic"
End Sub
End Class
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
Declare the following variables using the
appropriate data types:
a. Phone number
b. Age
c. Gender
d. Payment
e. Temperature
f. Radius
g. Address
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
Write a Program
to calculate the
total of 2
numbers
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
Public Class Form1
Dim num1 As Integer
Dim num2 As Integer
Dim total As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
num1 = TextBox1.Text
num2 = TextBox2.Text
total = num1 + num2
Label3.Text = total
End Sub
End Class
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
Part 2
APIIT City Campus – 2008 Feb – Last Modified by
Learning Outcomes
At the end of this lecture you should be able to:
1. Identify arithmetic operators and do simple
calculations
2. Apply the precedence of arithmetic operators
3. Able to use equality and relational operators
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
1. Arithmetic operators
2. Relational operators
3. Logical operators
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
Visual Basic Arithmetic Algebraic Visual Basic
Operation Operation Operation Expression
Addition + x+y x+y
Subtraction - Z-8 Z-8
Multiplication * yb y*b
Division (float) / v/u v/u
Exponentiation ^ none x^y
Unary Negative - -e -e
Unary positive + +e +e
Modulus Mod x%y X Mod Y
APIIT City Campus – 2008 Feb – Last Modified by Nadeera Ahangama.
Precedence of Arithmetic Operators
Operators Operation(s) Precedence
() parentheses Evaluated first
^ Exponentiation Evaluated second
+, - Sign operations Evaluated third
* or / Multiplication and Evaluated fourth
floating-point division
Mod Modulus Evaluated sixth
+ or - Addition and Evaluated last
subtraction
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
Suppose a = 2 , b = 3, c = 7 and x = 5
Determine Y:
Y=a*x^2+b*x +c
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
Visual Basic supplies a set of six relational
operators to compare the magnitudes of two
data values.
Comparisons can be made with numeric or
string values, with a Boolean value returned
from the comparison. The general format for
the comparison statement is
value operator value
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
Operator Use
= Tests whether two values are equal.
<> Tests whether two values are not equal.
< Tests whether the first value is less than
the second value.
> Tests whether the first value is greater
than the second value.
<= Tests whether the first value is less than or
equal to the second value.
>= Tests whether the first value is greater
than or equal to the second value.
APIIT City Campus – 2008 Feb – Last Modified by Nadeera Ahangama.
To handle multiple conditions more efficiently,
Visual Basic provides logical operators that
can be used to form complex conditions by
combining simple ones.
The logical operators are AndAlso, And,
OrElse, Or, Xor and Not.
We consider examples that use each of these
operators.
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
The Not operator returns True when the
condition is False. Otherwise, it returns
False.
Example
If Not (1 = 2) Then
MessageBox.Show("(1 = 2) is False. So Not False is True")
End If
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
The And operator returns True when the
conditions of the left and right are True.
Otherwise, it returns False.
Both conditions are evaluated before the
result is returned.
Example
If (1 = 1) And (2 = 2) Then
MessageBox.Show("(1 = 1) is True. (2 = 2) is True. So True
And True is True")
End If
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
The AndAlso operator returns False when the
condition on the left hand side is False.
Else, it returns True when the conditions of the
left and right are True.
Otherwise, it returns False.
The condition on the right hand side is never
evaluated when that on the left hand side is
False.
This is called short-circuited logic.
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
Condition1 Condition2 Condition1 AndAlso Condition2
True True True
True False False
False - False
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
The Or operator returns True when the
condition on either side is True.
Otherwise, it returns False.
Both conditions are evaluated before the result
is returned.
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
Condition1 Condition2 Condition1 Or Condition2
True True True
True False True
False True True
False False False
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
The OrElse operator returns True when the
condition on the left hand side is True.
Else, if the condition on the right hand side
is True, it returns True.
Otherwise, it returns False.
The condition on the right hand side is never
evaluated when that on the left hand side is
True.
This is called short-circuited logic.
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
Condition1 Condition2 Condition1 OrElse Condition2
True - True
False True True
False False False
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
The Xor operator returns True when the
condition on the left hand side or right hand
side is True, but not when both are True.
Xor means "Exclusive OR".
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
Condition1 Condition2 Condition1 Xor Condition2
True True False
True False True
False True True
False False False
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
APIIT City Campus – 2008 Feb – Last Modified by
Input Box
InputBox is used to get an input from the
user
Always contain OK and Cancel buttons.
Format
a = Inputbox (strprompt,strtitle,strdefault)
The text to Text on Value for
request input the title default
bar answer
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
Input Box
ComName = Inputbox ("What is the name of the company?",
"Company Request")
Company request X
What is the name of the company? OK
Cancel
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
InputBox("Enter an integer", "Input")
End Sub
End Class
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
Using a Message Box for Output
Class MessageBox for creating dialog
Format
Messagebox.Show (text as String, caption as String,
messageboxbuttons, messageboxicons)
MsgBox("Hi, Welcome to the System", MsgBoxStyle.Exclamation)
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
Example :
MessageBox.show("Are you ready for the report?",
"Report Request“, MessageboxButtons.YesNoCancel)
Report Request X
Are you ready for the report?
Yes No Cancel
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
Question and Answer Session
Q&A
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.
Next Session
Control Structures Part 1
APIIT City Campus – 2008 Feb – Last Modified by Nadeera
Ahangama.