18.7. Constructors


In that example, you will create a new instance of the Time class.

  Dim timeObject As New Time(  ); 

The parentheses look a lot like a method call. In fact, a method is invoked whenever you instantiate an object. This method is called a constructor. If you don't define one as part of your class definition, the compiler will provide one on your behalf.

The job of a constructor is to create the object specified by a class and to put it into a valid state. Before the constructor runs, the object is just a blob of memory; after the constructor completes, the memory holds a valid instance of the class.

Any constructor that takes no arguments is called a default constructor. It turns out that the constructor provided by the compiler takes no arguments, and, hence, is a default constructor. This terminology has caused a great deal of confusion. You can create your own default constructor, and if you do not create a constructor at all, the compiler will create a default constructor for you, by default.


If you do not explicilty initialize your member variables, they are initialized to their default values based on their type (integers to 0, strings to the empty string, etc.). Table 18-2 lists the default values assigned to various types.

Table 18-2. Types and their default values

Type

Default value

Numeric (Integer, Long, etc.)

0

Boolean

False

Char

The Unicode character whose code point is 0

Enum

0

Reference

Nothing


Typically, you'll want to define your own constructor and provide it with arguments, so that the constructor can set the initial state for your object. In Example 18-3, you will pass in the current year, month, date, and so forth, so that the Time object is created with meaningful data.

Example 18-3. Defining a constructor
 Public Class Time    ' Private variables    Private year As Integer    Private month As Integer    Private dayOfMonth As Integer    Private hour As Integer    Private minute As Integer    Private second As Integer    ' Public methods    Public Sub DisplayCurrentTime(  )         System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}", _             month, dayOfMonth, year, hour, minute, second)    End Sub 'DisplayCurrentTime    ' Constructor    Public Sub New( _     ByVal theYear As Integer, _     ByVal theMonth As Integer, _     ByVal theDate As Integer, _     ByVal theHour As Integer, _     ByVal theMinute As Integer, _     ByVal theSecond As Integer)       year = theYear       month = theMonth       dayOfMonth = theDate       hour = theHour       minute = theMinute       second = theSecond    End Sub End Class 'Time Module Module1    Sub Main(  )       Dim timeObject As New Time(2008, 7, 10, 9, 35, 20)       timeObject.DisplayCurrentTime(  )    End Sub End Module output: 7/10/2008 9:35:20 

You declare a constructor like any other sub except: the constructor is always named New.

If there are arguments to be passed, you define an argument list just as you would for any other method.

Example 18-3 declares a constructor for the Time class that accepts six integers.

In this example, the constructor (Sub New) takes a series of Integer values, and initializes all the member variables based on these parameters. When the constructor finishes, the Time object exists and the values have been initialized. When DisplayCurrentTime is called in Main, the values will be displayed.

Try commenting out one of the assignments and running the program again. You'll find that each member variable is initialized by the compiler. Integer member variables are set to 0 if you don't otherwise assign them. Remember, value types (e.g., integers) must be initialized one way or another; if you don't tell the constructor what to do, it will set innocuous values.



Programming Visual Basic 2005
Programming Visual Basic 2005
ISBN: 0596009496
EAN: 2147483647
Year: 2006
Pages: 162
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net