Constructor

  • Definition
    • A constructor in VB .NET is a special method named New that is automatically executed when an object of a class is created and is used to initialize the data members of the class.
  • Characteristics of a Constructor in VB .NET
    • It is mainly used to initialize data members of a class.
    • In VB .NET, the constructor is defined using the keyword Sub New().
    • The constructor name is always New.
    • It is executed automatically when an object of a class is created.
    • It is used to initialize variables.
    • It does not return any value.
    • If no constructor is defined, VB .NET automatically provides a default constructor.
    • It can be overloaded.
    • It can have access modifiers (Public, Private, Protected).
  • Syntax
Class ClassName
    Public Sub New()
        ‘ Constructor codes
    End Sub
End Class

Destructor

  • Definition
    • A destructor in VB .NET is a special method named Finalize() that is automatically invoked by the garbage collector to release unmanaged resources held by an object.
  • Characteristics of Destructor
    • In VB .NET, a destructor is implemented using the Finalize() method. Here, Name is always Finalize.
    • VB .NET does not support C++-style destructors.
    • Finalize() is rarely used now. In place of this method, the Dispose() method is preferred for resource management.
    • It is called automatically by the Garbage Collector.
    • It is used to free unmanaged resources (files, database connections, etc.)
    • It cannot have parameters.
    • It cannot be overloaded.
    • It cannot be called directly by the programmer.
    • Only one destructor per class is allowed.
    • Syntax
    Protected Overrides Sub Finalize()
        ‘ Cleanup code
        MyBase.Finalize()
    End Sub
    • Use of Destructor
      • Destructor is used
        • To close file handles
        • To release database connections
        • To free unmanaged memory
        • To clean up system resources

    NB : Destructor call depends on the Garbage Collector(GC), hence output may vary because garbage collection is automatic.

      ===============================================================
      ===============================================================

      Program Examples

      Example Constructor example in VB.NET to demonstrate the default and parameterized constructor concept.
      Imports System
      Module Module1
          Public Class Person
              Dim name As String
              Dim salary As Integer
      
              Public Sub New()
                  name = "Robert"
                  salary = 75000
              
                  Console.WriteLine(name)
                  Console.WriteLine(salary)
              End Sub
          End Class
      
          Sub Main()
              Dim obj1 As New Person()        
              Console.ReadKey()
          End Sub
      End Module
      
      Output:
      Robert
      75000
      
      ---------------  OR  -----------------
      
      Imports System
      Module Module1
          Public Class Person
              Dim name As String
              Dim salary As Integer
      
              Public Sub New()
                  name = "Robert"
                  salary = 75000
              End sub
              
              Public sub display()
                  Console.WriteLine(name)
                  Console.WriteLine(salary)
              End Sub
      
          End Class
      
          Sub Main()
              Dim obj1 As New Person()
              obj1.display()
      
              Console.ReadKey()
          End Sub
      End Module
      
      Output:
      Robert
      75000
      
      ---------------  OR  -----------------
      
      'Parameterized Constructor
      
      Imports System
      Module Module1
          Public Class Person
              Private name As String
              Private age As Integer
      
              Public Sub New(ByVal name1 As String, ByVal age1 As Integer) 
                  name = name1
                  age = age1
              End Sub
      
              Public Sub display()
                  Console.WriteLine(name)
                  Console.WriteLine(age)
              End Sub
          End Class
      
          Sub Main()
              Dim obj As New Person("John", 60)
              obj.display()
              Console.ReadKey()
          End Sub
      End Module
      
      Output:
      John
      60
      
      ---------------------  OR  --------------------
      
      Imports System
      Public Class Message
          Public Str As String
      
          Public Sub New(ByVal X As String)         
              Str = X
          End Sub
      
          Public Function display() As String
              Return Str
          End Function
      End Class
      
      Module Module1
          Sub Main()
              Dim con As New Message("Hello world")  'con object creation'
      
              Console.WriteLine(con.display())        'display method calling'
              Console.ReadKey()
          End Sub
      End Module
      
      --------------------------  OR  -----------------------------
      
      Imports System
      Module Module1
          Sub Main()
              Dim con As New Message("Hello world")  'con object creation'
      
              Console.WriteLine(con.display())        'display method calling'
              Console.ReadKey()
          End Sub
      End Module
      
      Public Class Message
          Public Str As String
      
          Public Sub New(ByVal X As String)         
              Str = X
          End Sub
      
          Public Function display() As String
              Return Str
          End Function
      End Class
      
      --------------  OR  ---------------
      
      Imports System
      Public Class Message
          Public x, y As Integer
      
          Public Sub New()
              Console.WriteLine("Function Return type Constructor")
          End Sub
      
          Public Sub New(ByVal m As Integer, ByVal n As Integer)
              x = m
              y = n
          End Sub
      
          Public Function display() As String
              Return x
          End Function
      
          Public Function output() As String
              Return y
          End Function
      
          Public Function final() As String
              Console.WriteLine(display())
              Console.WriteLine(output())
              Return 0
          End Function
      End Class
      
      Module Module1
          Sub Main()
              Dim obj1 As New Message()  'obj1 object creation'
              Dim obj2 As New Message(20, 30)
              obj2.final()
      
              Console.ReadKey()
          End Sub
      End Module
      
      Output:
      Function Return type Constructor
      20
      30
      ExampleA Constructor Example in VB.NET to demonstrate the concept of constructor overloading.
      Imports System
      Module Module1
          Public Class General
              Private x, y, z As Integer
              'Dim x, y, z As Integer
      
              Public Sub New()
                  Console.WriteLine("Constructor overloading")
              End Sub
      
              Public Sub New(ByVal x1 As Integer)
                  x = x1
      
                  Console.WriteLine()
                  Console.WriteLine(x)
              End Sub
      
              Public Sub New(ByVal x2 As Integer, ByVal y1 As Integer)
                  x = x2
                  y = y1
      
                  Console.WriteLine()
                  Console.WriteLine(x)
                  Console.WriteLine(y)
              End Sub
          End Class
      
          Sub main()
              Dim obj1 As New General()
              Dim obj2 As New General(100)
              Dim obj3 As New General(500, 1000)
      
              Console.ReadKey()
          End Sub
      End Module
      
      Output:
      Constructor overloading
      
      100
      
      500
      1000
      Example : A VB .Net program to show the Shared constructor concept.
      Imports System
      
      Class A
          Shared Sub New()      
              Console.WriteLine("Class A constructor initiated")     
          End Sub
      End Class
      
      Class B
          Inherits A
      
          Shared Sub New()
              Console.WriteLine("Class B constructor initiated")
          End Sub
      
          Public Shared Sub Message()
              Console.WriteLine("Hello world")
          End Sub
      
      End Class
      
      Module Testcons
          Sub Main()
              Console.WriteLine("50")
              B.Message()
              Console.WriteLine("100")
              Console.ReadKey()
          End Sub
      End Module
      
      Output:
      50
      Class B constructor initiated
      Hello world
      100
      Example : A VB .Net program to demonstrate the Constructor and Destructor concept.
      Class Sample
          
          'Destructor only
          Protected Overrides Sub Finalize()
              Console.WriteLine("Destructor called")
              MyBase.Finalize()
          End Sub
      End Class
      
      Module Module1
          Sub Main()
              Dim obj As New Sample()
              obj = Nothing
              GC.Collect()
              GC.WaitForPendingFinalizers()
          End Sub
      End Module
      
      -----------------  OR  -----------------
      
      'Constructor and Destructor Program
      Class Example
      
          ' Constructor
          Public Sub New()
              Console.WriteLine("Constructor is called")
          End Sub
      
          ' Destructor
          Protected Overrides Sub Finalize()
              Console.WriteLine("Destructor is called")
              MyBase.Finalize()
          End Sub
      
      End Class
      
      Module Module1
          Sub Main()
              Dim obj As New Example()   'Constructor executed due to Object creation
      
              obj = Nothing          'Object becomes eligible for destruction
              GC.Collect()
              GC.WaitForPendingFinalizers()
      
              Console.ReadLine()
          End Sub
      End Module
      
      -----------------  OR  -----------------
      
      
      Class Example
          Implements IDisposable   'Tells .NET that this class supports resource cleanup.
      
          Public Sub New()
              Console.WriteLine("Object Created")
          End Sub
      
          ' Dispose method release unmanaged resources manually
          Public Sub Dispose() Implements IDisposable.Dispose
              Console.WriteLine("Resources Released by Dispose() method calling")
          End Sub
      End Class
      
      Module Module1
          Sub Main()
              Dim obj As New Example()
      
              ' Explicitly releasing resources
              obj.Dispose()
      
              Console.ReadLine()
          End Sub
      End Module
      
      Output:
      Object Created
      Resources Released by Dispose() method calling
      
      NB: Here Dispose() called explicitly by programmer, not by Garbage Collector(GC).
      
      

      Loading


      0 Comments

      Leave a Reply

      Your email address will not be published. Required fields are marked *

      This site uses Akismet to reduce spam. Learn how your comment data is processed.