3.7. Creation and Destruction
The lifetime of an object running under .NET is not as straightforward as it is in other languages like C++ or managed VB.
In these languages, when an object is created, its constructor is called, which allows the object to be initialized. When the object goes out of scope, its destructor is called, providing a convenient place to free resources; this is usually the point at which database connections are closed, file handles are freed, and memory allocated during the object's lifetime is released. The object has a practical, convenient mechanism for cleanup. But as you will see, .NET handles things differently.
VB.NET does provide the means to declare a constructor similar to that of Java, C++, and most other OO languages. In this respect, VB.NET is similar to these languages. However, in VB.NET, the declaration of a constructor is a little more intuitive. The constructor for an object is a method named New, which makes perfect sense because this is what is called when you declare a new instance of an object. The following fragment calls the default constructor for the Hello class:
Dim hello As New Hello( ) 'Calls Hello.New( )
The default constructor does not have any arguments, and, like all constructors, it does not return a value:
Public Class Hello Public Sub New( ) Console.WriteLine("Hello, World!") End Sub End Class
In addition to a default constructor, you can define New so it takes any number of arguments. In fact, ...