Chapter 3
Chapter 3
Chapter:3
The class that used in method depends on the data type of the variable to
which assign the value.
Example:
quantityInteger = Integer.parse(quantityTextBox.Text)
priceDecimal = Decimal.parse(priceTextBox.Text)
„calculate the extended price
extendedPriceDecimal = quantityInteger * priceDecimal
6
Converting to String
When assign value to a variable, must assign like types.
ex. assign Integer value to an integer variable and decimal value to
a Decimal variable.
numberDecimal = convert.ToDecimal(numberSingle)
valueInteger = convert.ToInt 32(valueDouble)
amountSingle = convert.ToSingle(amountDecimal)
8
Constants
Constants are similar to variables
The difference is that in constant the value
contained in memory cannot be changed once
the constant is declared.
Example
Const X As Integer = 5;
9
Structures
A structure allows you to create your own custom data
types.
A structure contains one or more members that can be of
the same or different data types.
Each member in a structure has a name.
The syntax for structures:
[Public|Private|Friend] Structure varname
NonMethod Declarations
Method Declarations
End Structure
10
Cont.….
Example:
Public Structure Employee
Dim No As Long
Dim Name As String
Dim Address As String
Dim Title As String
End Structure 'Employee
11
Program flow Control
It is used to control the flow of program
executions
1. IF statement
Syntax:
If expression Then
Statement
Statement
…
End If
12
Cont.….
Example:
Dim A As Integer
Dim B As Integer
A = InputBox("enter the value of A")
B = InputBox("enter the value of B")
If A > B Then
MsgBox("A is greater than B")
End If
If A < B Then
MsgBox("A is smaller than B")
End If
If A = B Then
MsgBox("A is equal to B")
End If
NB: InputBox is a function that reads a value from the keyboard
13
Cont.….
2. IF…THEN…ELSE Statement
Syntax:
If expression Then
Statement
Statement
…
Else
Statement
Statement
…
End If
14
Cont.….
Example:
If 10 > 100 Then
' this message is never displayed
MsgBox("10 is greater than 100")
Else
' this message is always displayed
MsgBox("10 is smaller than 100, what a
surprise!!!")
End If
15
Cont.….
3. IF…THEN…ELSEIF Statement
Syntax:
If expression1 Then
Statement
Statement
…
ElseIf expression2 Then
Statement
Statement
…
ElseIf expression3 Then
Statement
Statement
…
Else
Statement
Statement
…
End If
16
Cont.….
Example:
If MyAge < 13 Then
' you must be a child
MsgBox("Child")
ElseIf MyAge < 20 Then
' you are a teenager
MsgBox("Hello Teenager")
ElseIf MyAge < 35 Then
' Your age is acceptable
MsgBox("Hi there young man")
Else
' the person is old
MsgBox("Hello there old man")
End If
17
Cont.….
4. Select Case
Its functionality is similar to the If…Then…Else
statement
We can use select case when we have more than two
Else…If statements.
The syntax for the Select Case statement is as follows:
Select[Case]expression
[ Caseexpressionlist
[statements]]
[ Case Else
[ elsestatements ] ]
End Select
18
Cont.….
Example:
Module decisions
Sub Main()
'local variable definition
Dim grade As Char
grade = "B"
Select case grade
Case "A"
msgbox("Excellent!")
Case "B", "C"
msgbox("Well done")
Case "D"
msgbox("You passed")
Case "F"
msgbox("Better try again")
Case Else
msgbox("Invalid grade")
End Select
msgbox("Your grade is {0}", grade)
End Sub
19
End Module
Loops
A loop statement allows us to execute a statement or
group of statements multiple times.
1. Do Loop:
Repeats the enclosed block of statements while a
Boolean condition is True
It could be terminated at any time with the Exit Do
statement.
20
Cont.…
Syntax: OR
Do{While|Until} condition
Do
[ statements ]
[ statements ]
[ Continue Do ] [ Continue Do ]
[ statements ] [ statements ]
[ Exit Do ] [ Exit Do ]
[ statements ] [ statements ]
Loop{While|Until} condition
Loop
Example:
'local variable definition
Dim a As Integer = 10
'do loop execution
Do
msgbox("value of a:" & a)
a = a + 1
Loop While (a < 20) 21
Cont.…
2. For Loop
Repeats a group of statements a specified number of times and a
loop index counts the number of loop iterations as the loop
executes.
syntax :
For counter = start To end [Step step]
[statements]
[Exit For]
[statements]
Next
Example:
Dim i As Integer
Dim val As Integer = 0
For i = 1 To 10
Val = val + 5
MsgBox("values are" & val)
Next 22
Cont.…
3. For…Each…Next Loop
Repeats a group of statements for each element in a
collection.
Syntax:
For Each element In group
[ statements ]
[ Exit For ]
[ statements ]
Next [ element ]
Example:
Dim An_array() As Integer = {1, 2, 3, 4, 5}
Dim i As Integer 'Declare i as Integer
For Each i In An_array
MsgBox(" Value of array is" & i)
Next 23
Arrays
An array is a data structure used to store
elements of the same data type.
The elements are ordered sequentially with the
first element being at index 0 and the last
element at index n-1
24
Cont.…
Declaring an Array
Dim arr(10) As Integer
Dim arr() As Integer = New Integer(10)
{}
Initializing an array
Dim arr() As Integer = {0,1,2,3,4}
To read or write an element in the array use the array’s
variable name followed by parentheses.
arr(0) = 5
i = arr(3)
25
Cont.…
We can use a for loop with an array:
Example:
Dim arr(5) As Integer
Dim i As Integer
For i = 0 To 4
arr(i) = I
MsgBox("the value of the array is " & i)
Next
We can find the upper and lower bound of an array
using Lbound and Ubound methods.
Example:
For i = LBound(arr) To UBound(arr)
arr(i) = i
Next
26
Cont.…
Multidimensional Arrays:
Arrays that have more than one dimension
To declare this array, use the following syntax.
Dim arr(3,5) As String
You can initialize Multidimensional Arrays during
declaration as follows.
Dim arr(,) As String = {{"11",
"12", "13"},{"21","22","23"}}
27
Cont.…
Dynamic Arrays
Arrays that can be modified (redimensioned) after
declaration.
The size of the array can be modified by using Redim
keyword.
Example:
Dim arr() As Integer
Dim i As Integer
ReDim arr(i)
For i = 0 To 3
arr(i) = i
Next
28
Functions
A procedure is a group of statements that together
perform a task when called.
VB.Net has two types of procedures:
Functions(Functions return a value)
Sub procedures or Subs(Subs do not return a value)
Defining a Function
The Function statement is used to declare the name,
parameter and the body of a function
29
Cont.…
Syntax:
[Modifiers] Function FunctionName
[(ParameterList)] As ReturnType
[Statements]
End Function
Modifiers − specify the access level of the function
FunctionName − indicates the name of the function
ParameterList − specifies the list of the parameters
ReturnType − specifies the data type of the variable the
function returns
30
Cont.…
Example:
Function FindMax(ByVal num1 As Integer,
ByVal num2 As Integer) As Integer
' local variable declaration */
Dim result As Integer
If (num1 > num2) Then
result = num1
Else
result = num2
End If
FindMax = result
End Function
31
Events
Events are basically a user action like key press, clicks,
mouse movements
An event is an action that calls a function or may cause
another event.
Event handlers are functions that tell how to respond to
an event
There are mainly two types of events:
Mouse events
Keyboard events
32
Handling Mouse Events
Mouse events occur with mouse movements in forms
and controls.
Following are the various mouse events
MouseDown, MouseEnter, MouseHover, MouseLeave,
MouseMove, MouseUp, MouseWheel
The event handlers of the mouse events get an argument
of type MouseEventArgs.
The MouseEventArgs object is used for handling mouse
events.
MouseEventArgs has the following properties:
Buttons, Clicks, Delta, X, Y 33
Cont.…
Example:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As
EventArgs) Handles MyBase.Load
' Set the caption bar text of the form.
Me.Text = "tutorialspont.com"
End Sub
34
Handling Keyboard Events
Following are the various keyboard events related with a Control class:
The event handlers of the KeyDown and KeyUp events get an argument of type KeyEventArgs.
Handled, KeyChar
Example: the following code handles KeyUP events of the text box called txtID.
Private Sub txtID_KeyUP(sender As Object, e As
KeyEventArgs) _
Handles txtID.KeyUp
36
Cont.…
An object is an entity that contains state (or data) and
behaviour (methods).
Objects are instances of a class.
The methods and variables that constitute a class are
called members of the class.
A class is a template for an object.
It is a data structure that can contain data members
such as constants, variables, and events and function
members
37
Cont.…
Class Definition
A class definition starts with the keyword Class followed
by the class name; and the class body, ended by the End
Class statement.
Syntax:
[ <attributelist> ] [ accessmodifier ] [
Shadows ] [ MustInherit | NotInheritable ] [
Partial ] _
Class name [ ( Of typelist ) ]
[ Inherits classname ]
[ Implements interfacenames ]
[ statements ]
End Class
38
Cont.…
The following example demonstrates a Box class, with three data
members, length, breadth and height
Module mybox
Class Box
Public length As Double ' Length of a box
Public breadth As Double ' Breadth of a box
Public height As Double ' Height of a box
End Class
Sub Main()
Dim Box1 As Box = New Box() ' Declare Box1 of type Box
Dim Box2 As Box = New Box() ' Declare Box2 of type Box
Dim volume As Double = 0.0 ' Store the volume of a box
' box 1 specification
Box1.height = 5.0
Box1.length = 6.0
Box1.breadth = 7.0
39
Cont.…
' box 2 specification
Box2.height = 10.0
Box2.length = 12.0
Box2.breadth = 13.0
'volume of box 1
volume = Box1.height * Box1.length * Box1.breadth
Console.WriteLine("Volume of Box1 : {0}", volume)
'volume of box 2
volume = Box2.height * Box2.length * Box2.breadth
Console.WriteLine("Volume of Box2 : {0}", volume)
Console.ReadKey()
End Sub
End Module
40
Constructors
A class constructor is a special member Sub of a class that is
executed whenever we create new objects of that class.
A constructor has the name New and it does not have any
return type
Example:
Class Line
Private length As Double ' Length of a line
Public Sub New() 'constructor
Console.WriteLine("Object is being created")
End Sub
41
Cont.…
Public Function getLength() As Double
Return length
End Function
Shared Sub Main()
Dim line As Line = New Line()
'set line length
line.setLength(6.0)
Console.WriteLine("Length of line : {0}",
line.getLength())
Console.ReadKey()
End Sub
End Class 42
Inheritance
Inheritance allows us to create new (derived) class from
the existing(base) class.
Syntax:
<access-specifier> Class <base_class>
...
End Class
Class <derived_class>: Inherits <base_class>
...
End Class
43
Cont.…
' Base class
Class Shape
Protected width As Integer
Protected height As Integer
Public Sub setWidth(ByVal w As Integer)
width = w
End Sub
Public Sub setHeight(ByVal h As Integer)
height = h
End Sub
End Class
' Derived class
Class Rectangle : Inherits Shape
Public Function getArea() As Integer
Return (width * height)
End Function
End Class
Class RectangleTester
Shared Sub Main()
Dim rect As Rectangle = New Rectangle()
rect.setWidth(5)
rect.setHeight(7)
' Print the area of the object.
Console.WriteLine("Total area: {0}", rect.getArea())
Console.ReadKey()
End Sub
End Class 44
Questions?
45