vb.net Constructor and destructor

4,609 views 17 slides Jan 29, 2019
Slide 1
Slide 1 of 17
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17

About This Presentation

uses of constructor and destructor in vb.net


Slide Content

Constructor and Destructor
BY LECTURER Suraj Pandey CCT college

Creating constructor and Destructor
BY LECTURER Suraj Pandey CCT
college
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.
If a class have a constructor, then the object of that class
will be initialized automatically.
To create a constructor for a class, create a procedure
named Sub New anywhere in the class definition. To
create a parameterized constructor, specify the names
and data types of arguments to Sub New just as you
would specify arguments for any other procedure.
Following program explains the concept of constructor:

BY LECTURER Suraj Pandey CCT
college

BY LECTURER Suraj Pandey CCT
college
When the above code is compiled and executed, it
produces the following result:
Object is being created
Length of line : 6

BY LECTURER Suraj Pandey CCT
college

Parameterized Constructor
BY LECTURER Suraj Pandey CCT
college
A default constructor does not have any parameter but if
you need a constructor can have parameters. Such
constructors are called parameterized constructors.
This technique helps you to assign initial value to an
object at the time of its creation as shown in the
following example:

The default constructor
Public Sub New()
Console.Writeline ("Dog is Created With Age Zero")
Age=0
End Sub
BY LECTURER Suraj Pandey CCT
college

The parameterized constructor
Public Sub New(val as Integer)
Console.Writeline ("Dog is Created With Age " +
Convert.ToString(val))
Age=val
End Sub
BY LECTURER Suraj Pandey CCT
college

BY LECTURER Suraj Pandey CCT
college

BY LECTURER Suraj Pandey CCT
college
When the above code is compiled and executed, it
produces the following result:
Object is being created, length = 10
Length of line set by constructor : 10
Length of line set by setLength : 6

BY LECTURER Suraj Pandey CCT college

Destructor
A Destructor is a special function which is called
automatically when a class is destroyed. In VB.NET, you
should use Finalize() routine to create Destructors.
A destructor is a special member Sub of a class that is
executed whenever an object of its class goes out of
scope.
A destructor has the name Finalize and it can neither
return a value nor can it take any parameters. Destructor
can be very useful for releasing resources before coming
out of the program like closing files, releasing memories
etc.
Destructors cannot be inherited or overloaded.
BY LECTURER Suraj Pandey CCT
college

BY LECTURER Suraj Pandey CCT
college
Following example explains the concept of destructor:

BY LECTURER Suraj Pandey CCT
college

BY LECTURER Suraj Pandey CCT
college
When the above code is compiled and executed, it
produces the following result:
Object is being created
Length of line : 6
Object is being deleted

Use of Constructor and Destructor
Constructors: Special methods which has following important points.
In VB.NET, this is defined as a Sub with the name of New.
No return type.
It's called whenever an object is created using the New statement.
Can be overloaded.
Can be private.
Destructors: Special methods Which has The following important points.
This is called Finalize in VB.NET.
VB.NET compiler creates a default Finalize method.
Can never be private.
By default is public.
Called automatically when an object releases it memory.
BY LECTURER Suraj Pandey CCT college

BY LECTURER Suraj Pandey CCT college
Tags