Professional Documents
Culture Documents
VB Unit 3
VB Unit 3
$1
Scanned by TapScanner
The event handlers of the
This object has the followingKeyDown and
properties KeyUp events get an argument of type
Handled - indicates if the KeyPress event is
handled KeyEventArgs.
KeyChar -stores the character corresponding
to the key pressed
CONSTRUCTOR
A
Constructor is a special type of Sub
AConstructor procedure called when an object is created.
method is called before an object of its
related class is created. If a class have a
constructor,then the object of that class will be initialized
A constructor is like as
instance
automaticaly.
method, but it
explicit return-type, and can be overridden to distinct from a method since it not ever has an
Thev have the job of initializing the provide custom initialization functionality.
that initializes an object to a object's data member s. If you combine a
valid state
your objects should remain in a valid state.and property methods that only allow valid constructor
states.
Syntax
Public Class User
Public Sub New)
End Sub
End Class
Properties of Constructor
1) It always has the name New".
2) It does not return any value.
3) It is creating the object stated by a class and
4) It does not use return type, not even void. placed your customn code it into a valid state.
5) It is automatically called when object of a class is
6) It isalways declaring in public section. created.
1. Default Constructor
A constructor having no parameters or arguments is called as
Default Constructor.
Example:
Module Modulel
Class Student
Public name, location As String
Public Sub New )
name =Alex"
location: "Pune"
End Sub
End Class
Sub Main )
Dim Student As Student New Student (0
Console. WriteLine (Student.name)
52
Scanned by TapScanner
Console. WritelLine (Student. location)
Console. ReadLine )
End Sub
End Module
Output:
Alex
Pune
2. Parameterized Constructor
A
constructor having parameters or arguments is
Example: called as Parameterized
Module Module1 Constructor.
Class Student
Public name,location As String
PublicSub New(ByVal a As String,
name=a
location-b ByVal b As String)
End Sub
End Class
Sub Main)
Dim Student As
Student-New Student(Alex""Pune")
Console.WriteLine(Student.name)
Console.WriteLine(Student.L ocation)
Console.ReadLine)
End Sub
End Module
Output:
Alex
Pune
Overloaded Constructor
A constructor having more (Multiple Constructor)
than one constructor is called as
Constructor. Overloaded Constructor or Multiple
Example
Module Modulel
Class Student
Public Name, location As String
Public Sub New)//Default Constructor
name-"Alex" location=Pune"
End Sub
Public Sub New(By Val a As String, ByVal b As
name-a String)/Parameterized Constructor
location=b
End Sub
End Class
Sub Main)
Dim Student As Student-New Student()/Default Constructor iscalled
I/Parameterized Constructor is called
Dim Student1 As Student-New Student("John'""Mumbai")
53
Scanned by TapScanner
Console.WriteLine(Student.name &"," &Student Location)
Console.ReadLinc()
End Sub
End Module
DESTRUCTORS
Destructors in VB.NET are executed as a
garbage collector will call Finalize() methodprotected method called as
directly.
Finalize (0
method cannot overloaded by but
if
implemented, but cannot Finalize).
call this
The
method
keyword in statement. Finalize(0 should be overridden by
method called automatically when using
required.
The main effort the object is Overrides
no longer
of the destructor is to
the current objects are going to be release resources and inform other
destroyed objects that
Syntax
Protected Overrides Sub Finalize)
End Sub
Example
Module Modulel
Public Class Student
Protected Overrides Sub Finalize()
Console .WriteLine("In Finalize Method")
Console.ReadLine0
End Sub
End Class
Sub Main)
Dim obj As New Student()
End Sub
End Module
End Function
EXCEPTION HANDLING
Exceptions are the incidence of some condition that changes the normal flow of
execution. For example, if your program run out of memory, file does not exist in the
given path, network connections are dropped etc., then it is a Runtime Errors.
Scanned by TapScanner
Logical Error: When an application does not perform
errors are difficult to find because a logic the way, it
error not generate the error was intended to
massage. perform. These
Structured Exception Handling
Anexception is a
is a response to a problem that occurs during the execution of a
special condition that ar1ses while program, An excenti en
attempt todivide by zero. a program is running, such
as an
Exceptions deliver a method to transter control from one
part ofa program to another.
VB.Net exception handling is built on four
Try - A Try block recognizes a keywords-Try, Catch, Finaly and Throw.
block of
be triggered. It's having one or more Catch code for which specific exceptions will
blocks.
Catch - A program catches an
a program where to handle the exception with an exception handler at the place in
of an exception. problem. The Catch keyword specifies the catching
Finally -The Finally block is used to execute a
an exception is thrown or not. For given set of
example, if you open a statements,
whether an exception is raised or not. it
whether
file, should be closed
Throw -A program throws an exception when a
using a Throw keyword. problem shows up. This is done
55
Scanned by TapScanner