Lesson 08.1 – Setting properties with a class constructor

Lesson Objectives

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.


Link to video on YouTube


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:

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:

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:

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:

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

or DropBox: Lesson 08.1 – https://www.dropbox.com/sh/f675fe9gxt1c50q/AADtzFT3_1xsijMy1bp6xuWya?dl=0


Next Lesson: Lesson 08.2 – Using class constructors with derived classes

Previous lesson: Lesson 07.1 – Inheritance and base classes

All lessons: Learn C# by Building a Simple RPG Index


13 thoughts on “Lesson 08.1 – Setting properties with a class constructor

  1. Hi again! I hate to comment on another Lesson (I did on 6.1) but may i ask whether you deleted the line,

    Location location = new Location(1, "Home", "This is your house.");

    as in the video it looked like you did, though, in the explanation underneath it doesn’t say… Sorry again!

    1. No problem 🙂 If you have a question, it’s always better to ask.

      I’m at work, so I can’t check the video right now. But I probably deleted the line because we don’t actually need it in SuperAdventure.cs. That was just a convenient, temporary place to show how to instantiate the object with parameters.

  2. Should I keep or delete the code then? Sorry I just don’t want to get a certain way through and for it not to work! Feel free to wait until you get back to work! Thanks!

    1. You can delete that line, because it instantiates an object that we never use. In a future lesson, you will replace all the constructor code anyway, to create the objects we actually use in the game.

  3. The “location” coding is quite confusing as it says when i hold my mouse cursor over it “Error: ‘ClassLibrary1.Location.Location(int,string,string)’ is inaccessible due to it’s protection level” what should i do? cant find any Faults in my coding :/


    1. When you see that error message, it’s because the program is trying to use a class, but the “scope” of the class is not set so it can be used by that part of the program.

      Check the Location.cs file with the code here (source code). I think the problem is probably that line 9 does not have the “public” in it.

      Please tell me if that does not fix the problem.

  4. My code keeps giving the error “Quest.Quest(int,string,string,int,int)’ must declare a body because it is not marked, abstract, extern or partial”

    public Quest(int id, string name, string description, int rewardExperiencePoints, int rewardGold);

    And i don’t know how to fix it. My code looks excactly like yours and i just can’t see the error /:

  5. Why does the Location class have to be put before the location object is defined but the Player class isn’t needed when _player object is defined?


    Location location = new Location(#, x, x);
    _player = new Player();


    Location location = new Location(#, x, x);
    Player _player = new Player();

Leave a Reply

Your email address will not be published. Required fields are marked *