When an object is destroyed, the Sub destruct method is automatically invoked by the system, but cannot be explicitly invoked. NET Framework can destroy an object when it thinks it has no use value. But remember, unlike class+terminate and Sub New, the system will call sub destruct this method after the last reference to an object.
1. The constructor of the sub New with parameters
Users can implement the constructor of a class by defining the Sub New procedure anywhere in the definition of a class. The first line of the constructor must be the constructor of the base class that called it, or another constructor of the current class. The user must confirm that the base class was initialized before the initialization of the other class. But surprisingly, even if you create a basic class, the user must call the constructor MyBase.New, because all classes are eventually inherited from a class named object. After invoking the constructor of its parent class, the user can add some additional initialization code to the Sub New procedure. Sub New can support constructors with parameters that are passed in from the procedure by calling the constructor, such as
Dim T as truck=new truck
2. Use sub destruct as destructor
To create a destructor for a class, a user can establish a procedure named sub destruct, and can enter any place in the definition of a class where code written in a destructor can be used to free other objects, close a file, or do other end work. The following example shows how to create a class with a constructor with parameters and initialize an attribute.
Imports System
Namespace trucknamespace
Module Module1
Class truck
Private iwheels as Integer
Sub New (initialwheels As Integer)
mybase.new
iwheels=initialwheels End
sub
sub-destruct ()
' place Cleanup code-here-end Sub-property wheels as Integer get wheels=iwheels end Get
Set
Iwheels=value
End Set End
Class
Shared Sub Main ()
Dim t as truck=new truck
Console.WriteLine ("The truck has" &_
t.wheels& "wheels when initialized") End Sub-end
Module