Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  OOP Basics - Constructors  »  Overloaded Constructors
OOP Basics - Constructors
by Ged Mead | Published  08/01/2009 | .NET Newbie | Rating:
Ged Mead

Ged Mead (XTab) is a Microsoft Visual Basic MVP who has been working on computer software and design for more than 25 years. His journey has taken him through many different facets of IT. These include training as a Systems Analyst, working in a mainframe software development environment, creating financial management systems and a short time spent on military laptop systems in the days when it took two strong men to carry a 'mobile' system.

Based in an idyllic lochside location in the West of Scotland, he is currently involved in an ever-widening range of VB.NET, WPF and Silverlight development projects. Now working in a consultancy environment, his passion however still remains helping students and professional developers to take advantage of the ever increasing range of sophisticated tools available to them.

Ged is a regular contributor to forums on vbCity and authors articles for DevCity. He is a moderator on VBCity and the MSDN Tech Forums and spends a lot of time answering technical questions there and in several other VB forum sites. Senior Editor for DevCity.NET, vbCity Developer Community Leader and Admin, and DevCity.NET Newsletter Editor. He has written and continues to tutor a number of free online courses for VB.NET developers.

 

View all articles by Ged Mead...
Overloaded Constructors

Introduction
If all we had at our disposal to create new instances was the default constructor then our toolbox will be pretty limited. We would also have to write a lot of verbose code to set those Properties, as shown in the previous examples. What we really need of course is a way to create new instances and - at the same time - assign useful values to the fields.

It's time to meet the Overloaded Constructor.

Take a look at this:

Code Copy

    Sub New(ByVal forename As String, ByVal surname As String)
        Me.Forename = forename
        Me.Surname = surname
    End Sub

Parameters and Signatures
This version of the constructor takes two parameters. Parameters are additional items of data that the procedure requires to carry out its tasks. You'll be familiar with these, I know, from standard Subroutines and Functions that you have written or used yourself.

Additional versions of class constructors are known as Overloaded Constructors. I've always thought that a much more descriptive name would have been to call them Alternative Constructors, because that's what they are. You can create as many Overloaded constructors as you want, subject to one simple rule. That is that the "signature" of each of the constructors must be different from the other constructors.

Let's quickly deal with this new technical term: - Signature. The signature defines the skeleton of a method. A constructor is just a specialised kind of method. The signature is comprised of the method name plus the types of any arguments required by the method.

So, taking the above constructor as our example, its signature is comprised of the procedure name "New" and the two types that are passed in as arguments "String, String". Note that it doesn't matter what names we assign to those string variables (forename, surname); it's the fact that there are two Strings which identifies the signature for this constructor. I could, for example, have called the two Strings in the parameters "firstname" and "lastname".

You will understand then that it is only permissible to have one Sub New method in the Person class that takes two strings, (and only two strings), as its parameters. But as long as you devise a different signature for each constructor, you can create as many variations of Person class constructors as you need.

Using the Overloaded Constructor
The client code for the overloaded constructor won't be foreign to you either. Something like:

Code Copy
        ' Create a new Person Instance
        Dim RealPerson As New Person("Ged", "Mead")

 

Once again, Intellisense will be available to help you - just as it is with the .NET built in classes:

 

You can then write code to display the results in a similar way to what we did previously:

Code Copy
        ' Create a new Person Instance
        Dim RealPerson As New Person("Ged", "Mead")
        '  Display
        Label1.Text = RealPerson.Forename & " " & RealPerson.Surname

 

The resulting display will be:

 

To keep things simple, the example overloaded constructor above took only two parameters. In a real application, it is much more likely that there would be a constructor which took values for all four of the Properties we have created.

Code Copy
    Sub New(ByVal forename As String, ByVal surname As String, ByVal DOB As Date, ByVal gender As String)
        Me.Forename = forename
        Me.Surname = surname
        Me.DateOfBirth = DOB
        Me.Gender = gender
    End Sub

 

Summary
In this series so far you have been introduced to the basics of creating your own class. We have created Fields, Properties and Constructors. These entities are usually described as class members.

When a Field is given Private scope, its data is kept hidden inside the class.

You saw that the Public Properties can be used as a way of stopping client code from directly accessing the Private Fields. Because we have given the Properties Public scope, client code can access them in order to read them or change the information represented by them.

You can use Property Procedures to Get and Set values on Properties, which will then be (optionally, validated - something we will look at later) and passed to the Private Field.

You saw that you can build more than one Constructor for a class. The constructor is called whenever a new instance of an object of this class is created. If there is more than one constructor, the additional ones are known as overloaded constructors and each must have a unique signature.

The signature of a Constructor is identified by looking at the name of the procedure and the Types of the arguments that it takes. In the case of constructors, the name will always be the word "New".

In the next part of the course we will add some functionality to the class.

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:3.09677419354838 out of 5
 62 people have rated this page
Article Score15737
Sponsored Links