Class Constructors
In code, you can frequently encounter a case where an object must be created and immediately after its properties must be set to a set of values. For example, if you continue with the Horse class:
Module Module1 Sub Main() Dim h As Horse = New Horse() h.m_name = "Lightning" h.m_color = "Brown" h.m_height = 1.9 Console.Out.WriteLine("Name is {0} ", h.m_name) Console.Out.WriteLine("Color is {0} ", h.m_color) Console.Out.WriteLine("Height is {0} ", h.m_height) End Sub End Module
The example is pretty common: Create an object and set the values for its fields. This task can become tedious, especially for classes with a large number of fields. That is one of the reasons why class constructors (or constructors, in short) were introduced ...
Get Visual Basic® .NET by Example 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.