Recipe 3.6. Initializing a Class Instance with Data


Problem

You want to ensure that some of the fields of a class are initialized before any of the exposed members of the class are used.

Solution

Add one or more custom constructors to your class.

Discussion

Constructors are Sub procedures named New:

 Public Sub New() End Sub 

A constructor with no arguments implements the default constructor. This is the constructor that is called anytime a new instance of the class is requested without additional initialization:

 Dim someEmployee As New Employee 

' Custom constructors include one or more arguments. This sample accepts an initial employee name and assigns it to the public Name field:

 Class Employee    Public Name As String = "Unknown"    Public Sub New(ByVal fullName As String)       If (Trim(fullName) <> "") Then Name = fullName    End Sub End Class 

One feature of classes is overloaded methods, which use the special Overloads key-word. This feature lets you use the same method name more than once in the same class, but have each method accept a different set of arguments. (See Recipe 3.14 for more information.) Constructors can also be overloaded, but they don't require the Overloads keyword:

 Class Employee    Public Name As String = "Unknown"    Public Salary As Decimal = 0@    Public Sub New(ByVal fullName As String)       If (Trim(fullName) <> "") Then Name = fullName    End Sub    Public Sub New(ByVal fullName As String, _          ByVal startingSalary As Decimal)       If (Trim(fullName) <> "") Then Name = fullName       If (startingSalary >= 0@) Then Salary = startingSalary    End Sub End Class 

Visual Basic calls the appropriate constructor based on the argument signature:

 ' ----- Uses the one-argument constructor. Dim someEmployee As New Employee("John Smith") ' ----- Uses the two-argument constructor. Dim someEmployee As New Employee("John Smith", 50000@) 

As an alternative way of doing the same thing, this sample class could have used an optional argument on a single constructor:

 Class Employee    Public Name As String = "Unknown"    Public Salary As Decimal = 0@    Public Sub New(ByVal fullName As String, _          Optional ByVal startingSalary As Decimal = 0@)       If (Trim(fullName) <> "") Then Name = fullName       If (startingSalary >= 0@) Then Salary = startingSalary    End Sub End   Class 

If you don't supply a default constructor but do supply constructors with arguments, any use of the class requires constructor arguments. If you want the arguments to be optional, either use the Optional keyword or include a default constructor with no arguments.

All classes must have a constructor, even classes that perform no specific initialization. Consider this empty class:

 Class Employee End Class 

Although you don't see a specific constructor, a default constructor is there, supplied on your behalf by the Visual Basic compiler. Any constructor you supply, default or with arguments, replaces the one added by Visual Basic.

All classes (except System.Object) derive from some other class. The default constructor for the base class is called implicitly from a derived class's constructor. Derived classes can also use a specific base-class constructor by calling it directly:

 Class Manager    Inherits Employee    Public Sub New()       MyBase.New("Unnamed New Employee")    End Sub End Class 

You can create instances of either classes or structures in your code. Modules cannot be instantiated, and therefore they do not use constructors.

See Also

Recipe 3.7 discusses destructors, which handle the end of an instance's lifetime instead of its beginning.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

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