O'Reilly logo

Visual Basic® .NET by Example by Bob Donald, Gabriel Oancea

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required