.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Constructor and destructor in vb.net

Posted By:satyapriyanayak       Posted Date: March 11, 2013    Points: 40    Category: Visual Studio    URL: http://www.dotnetspark.com  

constructor and destructor in vb.net
 

Here we will know how to use constructor and destructor in a windows application.

 

Constructor

 

Constructor is special method, which controls over the initialization of object. It is used for variable initialization. When we create an object of the class then the constructor within the class is automatically called. It is used with the keyword new before sub in vb.net

 

There are two types of constructor

1.Shared constructor

2.Instance constructor

 

Shared constructor: -

1.It is used to initialize only shared variables.

2.These are run only once during a single execution.

3.These constructors always get first preference in comparison to instance constructor.

4.These are used by the keyword shared before sub new.

5.It cannot take parameters.

 

Instance constructors: -

1.They are used to initialized instance variable ex dim, public, protected, friend, protected friend and private variable.

2.It can also initialize shared variable.

3.It can take parameters.

 

Ex: - How to declare Instance and shared constructors

 

    Public Class raj

        Public Sub New()

            MsgBox("Instant constructor of raj")

        End Sub

        Shared Sub New()

            MsgBox("Shared constructor of raj")

        End Sub

    End Class

 

 

 

Ex

Public Class Form1

    Inherits System.Windows.Forms.Form

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim a As New raj

        a.kunal()

        Dim b As New rahul("pintu")

        Dim c As New rahul(10, 10)

    End Sub

    Public Class raj

        Public Sub New()

            MsgBox("Hello raj constructor")

        End Sub

        Public Sub kunal()

            MsgBox("Hello kunal")

        End Sub

    End Class

    Public Class rahul

        Public Sub New(ByVal x As String)

            MsgBox("Hello" & x)

        End Sub

        Public Sub New(ByVal x As Integer, ByVal y As Integer)

            MsgBox(x + y)

        End Sub

    End Class

End Class

 

 

Destructor

 

Destructor release the memory of the object created by the constructor. It is of two types dispose and finalize .In dispose we have to intentionally call dispose destructor. But in finalize it is called automatically when the last reference of the object is released

 

Ex

 

Public Class Form1

    Inherits System.Windows.Forms.Form

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim a As New raj

        a.dispose()

    End Sub

    Public Class raj

        Public Sub New()

            MsgBox("Instant constructor of raj")

        End Sub

        Shared Sub New()

            MsgBox("Shared constructor of raj")

        End Sub

        Public Overloads Sub dispose()

            MsgBox("Dispose destructor of raj")

        End Sub

        Protected Overrides Sub finalize()

            MsgBox("Finalize destructor of raj")

        End Sub

    End Class

End Class



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend