Section 8.3. Constructors

   

8.3 Constructors

In Example 8-3, notice that the statement that creates the Time object looks as though it is invoking a method:

 Dim timeObject As New Time( ); 

In fact, a method is invoked whenever you instantiate an object. This method is called a constructor . Each time you define a class, you are free to define your own constructor, but if you don't, the compiler will provide one for you invisibly and automatically.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.

The Time class of Example 8-3 does not define a constructor. As noted earlier, if you do not declare a constructor, the compiler provides one for you. The constructor provided by the compiler creates the object but takes no other action.

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 explicitly initialize your member variables , they are initialized to innocuous values (integers to 0, strings to the empty string, etc.). Table 8-2 lists the default values assigned to primitive types.

Table 8-2. Types and their default values

Type

Default value

Numeric (Integer, Long, etc.)

Boolean

false

Char

`\0' (null)

Enum

Reference

null

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 8-3, you want to pass in the current year, month, date, and so forth, so that the object is created with meaningful data.

You declare a constructor like any other member method except:

  • The constructor is always named New.

  • Constructors are declared using the Sub keyword (which means there is no return value).

If there are arguments to be passed, you define an argument list just as you would for any other method. Example 8-5 declares a constructor for the Time class that accepts a single argument, an object of type DateTime. (DateTime is a type provided by the .NET Framework Class Library.)

Example 8-5. Creating a constructor
 Option Strict On Imports System Public Class Time    ' Private variables    Private Year As Integer    Private Month As Integer    Private Date 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, Date, 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       Date = theDate       Hour = theHour       Minute = theMinute       Second = theSecond    End Sub End Class 'Time Module Module1    Sub Main( )       Dim timeObject As New Time(2005, 3, 25, 9, 35, 20)       timeObject.DisplayCurrentTime( )    End Sub End Module 
  Output:  3/25/2005 9:35:20 

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 are 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 to 0. Integer member variables are set to if you don't otherwise assign them. Remember that value types (e.g., integers) must be initialized; if you don't tell the constructor what to do, it will set innocuous values.

   


Learning Visual Basic. NET
Learning Visual Basic .Net
ISBN: 0596003862
EAN: 2147483647
Year: 2002
Pages: 153
Authors: Jesse Liberty

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