Calling the Default Constructor

As mentioned earlier, if you do not create a constructor, an implicit default constructor will be called by the compiler. You can see this at work by commenting out the constructor in Example 7-1:

'Public Sub New( _
'   ByVal xCoordinate As Integer, ByVal yCoordinate As Integer)
'    myXVal = xCoordinate
'    myYVal = yCoordinate
'End Sub 'New

and replacing the first line in Main( ) with one that creates an instance of Location without passing values:

'Dim loc1 As New Location(200, 300)
Dim loc1 As New Location( )

Because there is now no constructor at all, the implicit default constructor is called. The output looks like this:

Loc1 location: 0, 0
Loc2 location: 0, 0

The default constructor has initialized the member variables to zero.

Get Programming Visual Basic .NET, Second Edition 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.