Construction and Destruction

Many types of objects require some code to run every time the object comes into existence. This code typically sets properties to default values, initializes tables, gets information about the current operating environment, and so forth. To make this code run automatically, you can provide a constructor method.

Constructor methods are optional but no class can ever have more than one of them. In Microsoft Visual Basic .NET, a constructor method is always a subroutine named New. The following code shows a typical constructor method:

Public Class Deck
    Public Sub New()
        InitializeDeck()
    End Sub
    Public Sub InitializeDeck()
'       Contents of InitializeDeck procedure
    End Sub
'       Other properties and methods
End Class

The constructor ...

Get Faster Smarter Beginning Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.