Code - Financial Independence

C#/Visual Basic – Constructors

Now that you have an overview of what a class is, we can dive a bit more into the subject. Constructors is the first thing I want to explore with you.

Remember when I was talking about the car example, let’s say we have a Car and it comes with different options. When the factory builds the car for you, they can choose which package this car will be equipped with, that is : Limited, sport, base, and so on. A constructor for a class is similar in a way that you will choose how the class will be built to suit your needs.

Rules of the constructors!

1. You can add multiple constructors to a class.
2. When you create and instance of this class (a real object), you can only call a constructor once.
3. Constructors are defined by their parameters for example, string ID, int Age is the same as string Name, int Age. In other words, it won’t let you create two constructors with the same parameters because even if the variable names are different, C# will not know which one to apply.
4. A constructor as the name of the class(C#) or the name New (Visual Basic).
5. A constructor has no return value.
6. It can be either public or private (in some rare cases).

We’ve seen how to declare a constructor in the last tutorial, now we can try to use multiple constructors at the same time! We can create a new Console application and start applying this knowledge.

Here is an example following the rules mentioned above.

Let’s create a new class like in the last tutorial named User. This will be our blueprint to build constructors in.

Visual Basic

Public Class User
Public Property FirstName As String
Public Property LastName As String
Public Property Age As Integer
Public Sub New()
End Sub
End Class

We created a few Properties and an empty constructor. A constructor with no parameters like the one in the preceding example is called a Default Constructor. Your class will still have a default constructor even if it’s not explicitly declared but it won’t do anything. What we want to do now is to create a standard User, with standard parameters.

We might want a default constructor to put values in our properties to start with, just like a car’s base model will have a default equipment package.

Visual Basic

Public Class User
Public Property FirstName As String
Public Property LastName As String
Public Property Age As Integer
Public Sub New()
FirstName = "Alexandre"
LastName = "Sauve"
Age = 25
End Sub
End Class

So that’s it for our User class for now, let’s test this out and see what it gives us. You can go back Program.vb/Program.cs (remember that I constantly rename the Module1.vb file to Program.vb in Visual Basic for consistency with C#).

Notice that the new User() statement is in fact calling our constructor to create a new User. Since there are no parameters inside the parenthesis, the programming language knows that it has to call the default one. It will then create a new User with our defined default construction.

Yes, that’s exactly what we’ve coded in the default constructor!

Now let’s step up the game a bit, we’ll create a parameter constructor to let the user of our class decide what he wants to assign to each property. The only difference here is that we want to let the user input parameters instead of an empty () when he initializes a new User objet.

We can go back to our User class.

Visual Basic

Public Class User
Public Property FirstName As String
Public Property LastName As String
Public Property Age As Integer
Public Sub New()
FirstName = "Alexandre"
LastName = "Sauve"
Age = 25
End Sub
Public Sub New(FirstName As String, LastName As String, Age As Integer)
Me.FirstName = FirstName
Me.LastName = LastName
Me.Age = Age
End Sub
End Class

Here we go! Same method name as you can see (class name for C# and New for Visual Basic), again no return value. The only thing that changed in this version is the fact that we have parameters.

When the object will be created, we will now have the choice of two constructors. Here is an example.

If we add more constructors, it will populate the list and give the user more choices on how to build the object.

We can now create even more constructors!

Remember that you can build as many constructors as you want, as long as their signatures are different (parameter type). Constructors are used to build an initial state for your object, meaning you can use them to set parameters before the object is accessed.

In the next tutorial we’re going to explore Properties in more details!

Get the latest from Unaura

RSS Feed

Information

Unaura Online Systems is a business doing Web development and hosting of applications like Unra. It maintains a blog to share knowledge with people about .Net C#/Visual Basic code and financial independence.