Lesson 09.1 – Using your classes as datatypes

Lesson Objectives

At the end of this lesson, you will know…

  • How to create a property in a class that uses one of your other classes as its datatype.


Adding properties with custom datatypes

Currently, all of the properties in our classes are either strings or integers. But sometimes we need to store something more complex in a property.

For example, in the Location class, we want to possibly store an item required to enter the location (such as a key). If there is a quest available at that location, we need to store that value. We also might have a monster that exists in that location.

So we need to add some new properties to the Location class, to store these values.


Link to video on YouTube


Step 1: Start Visual Studio Express 2013 for Desktop, and open the solution.

Step 2: Edit the Location class, in the Engine project. Add these new properties:

Now we have properties to hold these values for each location. Because we need to store an Item object in the ItemRequiredToEnter property, its datatype is Item. Just like using a string datatype when we want to store some text in a property.

Step 3: Now, let’s add parameters to the constructor to accept some of these values and save them to the properties. Change the constructor code to this:

Notice that after each of the new parameters, there is ” = null”. Some locations won’t have an item required to enter them, a quest available at them, or a monster living there. This lets us call the Location constructor without passing these three values. The constructor will know to use the default values, which, in this case, is the “null”. So, both of these lines would work the same way:

Step 4: Edit the Quest class, in the Engine project. Add this new property:

The RewardItem property will store what item the player receives when they complete the quest.

NOTE: In the video, the parameters in the Location class constructor are on two lines now, and that’s OK. Since the last character of the first line is a comma, the computer knows that the next line is a continuation of the first line. I just did this because that line was getting long, and a little difficult to read.

In C#, the end of a line is signified by a semi-colon “;”, a closing parentheses “)”, or a closing bracket “}”.



Now you can create properties that hold more complex values. This way, you’ll be able to do things such as get the name of the monster at the location, without needing to do extra lookups somewhere outside of your object.


Source code for this lesson

Get it from GitHub: https://gist.github.com/ScottLilly/5306227a2128e0e84903

or DropBox: Lesson 09.1 – https://www.dropbox.com/sh/4v0yjmf749lwxiq/AAAZ5qhnMMl3An489r9-N68ka?dl=0


Next lesson: Lesson 10.1 – Creating collections of objects

Previous lesson: Lesson 08.2 – Using class constructors with derived classes

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

19 thoughts on “Lesson 09.1 – Using your classes as datatypes

  1. Step 4 says to add this to the Quest class:
    public List QuestCompletionItems { get; set; }

    I understand now, but it was confusing at the time because we don’t yet have a QuestCompletionItem class so this line generates an error. In lesson 10.1 we create the QuestCompletionItem class, and then in step 4 of lesson 10.1 you repeat the instruction to add this list to the Quest class, so you could probably delete this from this lesson.

    Also the video doesn’t show you entering the 4 “public Location LocationToNorth { get; set; }” properties, or doing step 4.

    I’m really enjoying the tutorial, thanks!

    1. Thanks for pointing that out. When I was making the lessons, I ended up moving some parts around, from one lesson to another.

  2. Why don’t we put LocationToNorth and the like to public Location(…..) ?? I’m confused why this is so. Also, a bit back track but why do we have private Player _player when we don’t do it for any other class?

    1. There are a couple reasons I did it this way.

      First, if we had parameters for the LocationToNorth/South/East/West values in the constructor, then we’d be required to pass in values for all four parameters when we instantiate a Location object. And most locations don’t have another location in all four directions.

      We could get around that by passing in “null” as the value for directions that don’t go to another location, but I didn’t want to introduce that in this lesson.

      Second, in order to create the “home” Location object, we’d need to have already created an object for the “town square” (to pass in as its LocationToNorth). However, we wouldn’t be able to create the “town square” object until we created the “home” object, because the “home” Location would need to be passed in as its LocationToSouth.

      This is called a “circular dependency”. You can’t create A until you have B. But you can’t create B until you have A.

      When you get to Lesson 11.1 “Using a static class”, the PopulateLocations function creates the game’s locations and sets them up with their connected locations.

      I put the answer to your other question in the comments for Lesson 05.1, as a response to your question there.

  3. In “Location.cs” code, this line:

    public Location(int id, string name, string description, Item itemRequiredToEnter = null, Quest questAvailableHere = null, Monster monsterLivingHere = null)

    On the sections “= null” there are red lines saying “Default rarameter specifiers are not permitted”.

    Is this normal? When I build the program, three errors are counting.

    1. It sounds like your project is using the 3.5 .Net framework, instead of the 4.0 version. Try doing this:

      1. In Visual Studio’s Solution Explorer (upper right section) right-click on the “Engine” project and select “Properties”.
      2. In the “Target framework” section, select “.Net Framework 4”.
      3. Save the solution and try to re-compile it.

      Let me know if that works for you, or not.

  4. Hi,

    First of all, I really enjoy this tutorial. I have some basic C# knowledge and your tutorial helps me understand the more advanced stuff.

    I noticed that the code written in this lesson, at step 4 cannot be found in the source code from the next lesson. Is this correct?



  5. You mention in Step 4: Add this line of code to Quest class: ”

    public Item RewardItem { get; set; }


    but in the next tutorial (Video 10.1) at 6:52 when you go to the Quest Class, that line of code is not in there. I worry I might have overlooked something.

    1. Hi Bryan,

      Thanks for catching that and letting me know about it. The RewardItem should still be in the Quest class. I’ve added a note to the lesson, and the video, to let everyone else know.

  6. what is this: public List QuestCompletionItems { get; set; }
    My program doesnt compile and i don’t see this in video or in under video.
    Please help me to understand

  7. Hello

    Could you explain me why property functions use class name as data type?
    because I don’t understand how it works.
    Thank you.

    1. Are you asking about the datatype for a property, or for a function?

      For a property: A property is used to hold a value. We need to declare the type of data (datatype) to store in that property. If we want to store an integer in the property, the datatype is “int”. If we want to store a string, the datatype is “string”. But, we can also store other types of objects in a property. In the code for the Location class, we want to be able to say that a monster lives at a Location. So, we will create an instance of a Monster object. We want to store that Monster object in a property. So, we need to declare that property’s datatype as Monster – because it will hold a Monster object.

      For a function: The datatype for a function tells what type of object the function will return, after the function does its work. If the function does not return a value, you use “void” for the datatype.

      Does that make it clear for you?

Leave a Reply

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