The Constructor Method*

The constructor is the New method

When you create an instance of a class like this …

p3 = New Personnel.Person

…you are actually calling a special built in method of the class which has the name New. You cannot see the code for this method in the class, but it is there!  The New method is known as the constructor.

NOTE:
Every class that you write has a constructor.  It is inherited from a built in Object class behind the scenes.

Override the original constructor

You can replace the class’s built in constructor with a different version of your own, or to be more precise, you can override the original. In this task you will see how  to implement a customised version of your Person class’s constructor that allows a front end application to set property values at the time of instantiation, like this.

p3 = New Personnel.Person(“Emma”, “Peel”, #2/2/1956#)

Steps to follow:

  1. Ensure that your Personnel project and your front end (the Windows Forms Application that contains the programs a user can interact with from the previous task) are both open in separate instances of Visual Studio.
  2. Implement a customised constructor for your Person class as shown here:

Overloading a constructor

This constructor you have just written has three parameters. You can see that the parameter values are being used to set the property values when the class is instantiated. These parameters must be passed when the class is instantiated, they are not optional.

If you were to now leave your Person class as it is, your customised version of the constructor would effectively replace the original constructor, and the parameters would always be mandatory. In order to make these parameters optional, you can overload the constructor with a second version that effectively does the same as the original constructor.

Steps to follow:

  1. Implement an overload for the constructor for your Person class as shown here (it is just an empty stub for a procedure, which also has the name New):

  1. Rebuild your Personnel solution.
  2. Modify your front end by instantiating a third Person, p3 as shown here. This time you are passing values for the properties of p3 via the constructor.

NOTE
When you were typing, you should have noticed that there are two versions of the constructor available, one which expects parameters, and another that does not. Without the overload that does not expect any parameters, it would have been mandatory to set the property values upon instantiation.

  1. You can still change any of the property values that were set when the constructor was called. Add another line of code to change the DateOfBirth of Person p3, and some extra code to output their details along with the other people as shown here:

  1. Test your font end.

NOTE:
You have just seen that the class constructor is useful for setting property values upon instantiation. A constructor could do much more. For example, you could pass it a person’s ID which is then used to interrogate a database from which other property values can be initialised.

Extension Tasks

Remove the constructor overload that does not expect parameters from the Person class, rebuild the project and examine the impact on your front end. Repair this ‘damage’ by replacing the overload when you have seen the effect.

Add a new property to your Person class for Gender. Make all the necessary changes to your SaveToFile method and your front end that are now necessary and test your application.

Add validation to your Gender property to report if Male or Female has not been used.

Change the validation in your DateOfBirth property to ensure a person’s age is at least 21. Did this require any changes to the front end?

NOTE:
As long as you do not change a class’s public interface (i.e. the names of the methods and properties, the parameters they expect, or their return types) you can modify the code within these methods and properties without the need to change the code in the front end.

Write an overload of the constructor which takes a person ID as a parameter, and uses it to retrieve and assign property values that have previously been saved to a database (as shown below).  Needless to say, the database must already exist and some property value already saved into it (see the previous exercise on method overloading).