Constructors

 <  Day Day Up  >  

Sometimes it is desirable to do some initialization of a type when it is created. Constructors are methods that are run when an instance of a type is created, allowing for user -defined initialization. Constructors look exactly like regular methods (discussed in the next chapter) except that they have the special name New . For example:

 Class Customer   Public Name As String   Public Address As String   Public PhoneNumber As String   Public Sub New(ByVal Name As String, ByVal Address As String)     MyBase.New()     Me.Name = Name     Me.Address = Address   End Sub End Class Module Test   Sub Main()     Dim Bob As Customer     Bob = New Customer("Bob", "134 Main Street")   End Sub End Module 

In this example, the Customer class defines a constructor that takes two parameters: a customer name and a customer address. It then initializes the Name and Address fields with these values. As shown in the example, arguments can be supplied in a New expression and are passed off to the constructor parameters. Like methods, constructors can be overloaded. If a class or structure doesn't define a constructor, the language will assume that the type has a constructor with no parameters that does nothing.

Unlike methods, constructors are not inherited ”each class must declare its own constructor. To ensure that base classes have a chance to run their constructors, a constructor in a derived class automatically calls its base class constructor first. If there is no parameterless base constructor, the constructor must begin with a call to another constructor in the same class ( Me.New(...) ) or to another a constructor in the base class ( MyBase.New(...) ). This ensures that each base class, all the way up to Object , has a chance to do proper initialization. For example:

 Class Person   Public Name As String   Public Sub New(ByVal Name As String)     MyBase.New()     Me.Name = Name   End Sub End Class Class Customer   Inherits Person   Public Address As String   Public PhoneNumber As String   Public Sub New(ByVal Name As String, ByVal Address As String)     MyBase.New(Name)     Me.Name = Name     Me.Address = Address   End Sub   Public Sub New(ByVal Name As String, ByVal Address As String, _     ByVal PhoneNumber As String)     Me.New(Name, Address)     Me.PhoneNumber = PhoneNumber   End Sub End Class 

In this example, Customer provides two constructors: one that specifies a phone number and one that doesn't. The one that provides a phone number simply calls the one that doesn't to initialize the Name and Address fields before setting the PhoneNumber field.

Advanced

Field initializers are assigned immediately after the constructor call. This ensures that any values that they depend on from the base type are available.


Structure Constructors

Structures can have constructors too, even though structures never have to be allocated using New . Instead of creating a new instance on the heap, a structure constructor creates a new instance of the structure on the stack. This structure can then be assigned to a value type variable or passed as an argument to another function. For example:

 Structure Point   Public x, y As Integer   Public Sub New(ByVal x As Integer, ByVal y As Integer)     Me.x = x     Me.y = y   End Sub End Structure Module Test   Sub PrintPoint(ByVal p As Point)     Console.WriteLine(p.x & "," & p.y)   End Sub   Sub Main()     Dim p1 As Point = New Point(10, 20)     PrintPoint(New Point(20, 30))   End Sub End Module 

In this example, the Point constructor simplifies the process of creating a new Point value. The first statement in Main assigns the Point value (10, 20) to the variable p1 , while the second statement passes the Point value (20, 30) to the PrintPoint subroutine.

Unlike classes, structures cannot define a parameterless constructor. In the .NET Framework, a structure's parameterless constructor represents its default value. The following example initializes a Point variable with the default value of the Point type, (0,0) .

 Module Test   Sub Main()     Dim p1 As Point     p1 = New Point()   End Sub End Module 

Shared Constructors

Constructors are primarily used to initialize instances of types, but they can also be used to initialize shared information in a type. A shared constructor is declared the same way as a regular constructor except that it uses the Shared modifier, can have no parameters, and cannot be declared with an access level. The last two restrictions are due to the fact that a program never explicitly calls a shared constructor ”the .NET Framework runtime will automatically call the shared constructor when necessary. For example:

 Class Customer   Public Prefix As String   Public Name As String   Shared PrintPrefix As Boolean   Shared Sub New()     PrintPrefix = True   End Sub End Class 

Advanced

When shared constructors are run is determined by the .NET Framework, but the general rule of thumb is that the shared constructor will be run before anything that could depend on it can be accessed.


 <  Day Day Up  >  


The Visual Basic .NET Programming Language
The Visual Basic .NET Programming Language
ISBN: 0321169514
EAN: 2147483647
Year: 2004
Pages: 173
Authors: Paul Vick

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