At the end of this lesson, you will know…
- What a class constructor is
- How to use a class constructor to set the properties of an object, during the object’s instantiation
- How a child (derived) class can call the constructor of its base class
Let’s clean up the code a little
Right now, in the SuperAdventure class, the code instantiates a Player object, then has five lines to set the properties of the player object. That works fine, but we can make that a little cleaner by doing all that in one line of code.
We do this by creating a new class constructor.
Every class starts with a default constructor. It’s what is run when you call “new Player()”. It builds and returns a new Player object.
However, we can make our own constructors that do more than this default action. So, we are going to make constructors that accept the values we want to store in the properties.
Step 1: Start Visual Studio Express 2013 for Desktop, and open the solution.
Step 2: To see how this works, we’re going to create a location object in our UI code (for a while). In the SuperAdventure project, right-click on SuperAdventure.cs and select “View Code”.
Step 3: Go to the end of line 21 and hit the enter key twice (to give us a little space). Then add in these lines:
Location location = new Location();
location.ID = 1;
location.Name = "Home";
location.Description = "This is your house.";
Here, we create a new Location object, and save it to the variable “location” (notice that the variable name is all lower-case, to make it different from the class). Then we assign values to the property of that object.
Step 4: Double-click on the Location class, in the Engine project, to start working with it. Go to the end of line 13 and hit enter twice. Then, add this code:
public Location(int id, string name, string description)
ID = id;
Name = name;
Description = description;
This is our new constructor code.
It starts out with “public”, because we want to be able to create a new Location object from anywhere in the solution. Then it has the name of the class. After that, we see three “parameters” between the parentheses.
When you run a function, or method (like a constructor), it can accept values. These are called parameters. In C#, you need to declare the datatype of the parameter and its name. In this constructor, there are three parameters: id (an integer), name (a string), and description (another string).
Notice that the names of the parameters match the names of the properties, except they are all lower-case. By having a different case from the property, it’s more obvious when you’re working with the property and when you’re working with the parameter value. They don’t need to match the property name, but I do that to make it obvious what the parameters are going to be used for.
So, now when you call the Location constructor, you’ll need to pass in these three values.
On lines 17 through 19, we assign the values of the parameters to the properties in the class. Notice that the parameters (lower-case) are all on the right of the equal sign, and the properties (mixed-case) are on the left. The value on the right of the equal sign gets assigned to the property, or variable, on the left.
Step 5: Go back to the SuperAdventure code and you’ll see there is a red line under the “new Location()” code. That’s because we aren’t using the default constructor in the Location class anymore – we have our own custom constructor that wants three parameters.
If you remove the “new Location()”, and re-type it, you’ll see that as soon as you type the open parentheses “(” there is a tooltip that shows you the “method signature” (the parameters you need to pass to this method/constructor).
Change line 23 to this:
Location location = new Location(1, "Home", "This is your house.");
Now, when you construct a new Location object, you can pass in the parameters you want assigned to the properties. This makes your code a little shorter/cleaner. It is also a way of double-checking that you’ve set all the properties. If you manually set each one, like before, it’s easy to forget one. But with constructor that accepts parameters, you’ll know exactly how many have to be passed to it. If you forget one, you’ll see that red line.
Step 6: Double-click on the Quest class, in the Engine project, to start editing it. Add the following lines after the properties:
public Quest(int id, string name, string description, int rewardExperiencePoints, int rewardGold)
ID = id;
Name = name;
Description = description;
RewardExperiencePoints = rewardExperiencePoints;
RewardGold = rewardGold;
Now the Quest class has a custom constructor that accepts values to use to set its properties.
You’re going to see custom constructors in many C# projects. It’s an easy way to ensure that your class has all needed properties set. You can also create one to do some other special tasks, which we’ll see a bit later.
Source code for this lesson
Get it from GitHub: https://gist.github.com/ScottLilly/9d6042780a563cabef12
Previous lesson: Lesson 07.1 – Inheritance and base classes
All lessons: Learn C# by Building a Simple RPG Index