Lesson 03.3: Creating the Player Class



  • A program represents activities in “the real world”.
    • Or, for us, the game world.
  • The program needs to know how to represent “things”.
    • To find the “things” in a program, look for the nouns in the requirements.
    • We will create “classes” for each of those things.
    • We will also create other classes. But, for now, we will focus on the “things”.
    • Another term for these classes is “model”, because they are a “model” of something in the real world. They are also sometimes called “business objects”.
  • A class is an outline of a “thing”.
    • It is like a blank form.
    • It has “properties” – details about the “thing”. These are the boxes you would fill in, on a form.
  • The “properties” have a “datatype”, or “type”.
    • This tells the program what type of information can be stored in the property.
  • Some common datatypes are:
    • “string” which hold letters, numbers, punctuation, spaces, etc.
    • “int” which holds whole (integer) numbers
    • “decimal” which holds numbers with decimal values, like 1.5, 2.7
    • “DateTime” which holds dates and times
  • Properties hold values.
    • You can put a value into a property – using “set”.
    • You can read a value from a property – using “get”.


  • We will put our “models” in a separate project in the solution
    • This solution is a “class library”, and will hold all the classes for our models.
    • We want the “models” separate from the “view” (the WPFUI). This makes it easier to re-use, or test, these classes (which we will do later).


Source Code

Create a new project, to hold the “models”

Step 1: Right-click on the SOSCSRPG solution, and select Add -> New Project…

Step 2: If you’re using Visual Studio 2015, choose a Visual C# -> Windows -> Class Library project. If you’re using Visual Studio 2017, use Visual C# -> Windows Classic Desktop -> Class Library (.NET Framework). Name this project “Engine”. Delete the Class1.cs file from the project.

Step 3: Right-click on the Engine project, and select Add -> New Folder. Name it “Models”

Step 4: Right-click on the “Models” folder, and select Add -> New Item -> Visual C# Items -> Class. Name the class “Player.cs”

Step 5: Create this code, in the Player.cs file, to define the properties of the Player class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Engine.Models
    class Player
        string Name { get; set; }
        string CharacterClass { get; set; }
        int HitPoints { get; set; }
        int ExperiencePoints { get; set; }
        int Level { get; set; }
        int Gold { get; set; }


Return to main page

18 thoughts on “Lesson 03.3: Creating the Player Class

  1. Hi Scott,

    For those with Visual Studio 2017 – there are multiple options for Class Library project type. The correct one appears to be the C# project template “Class Library (.NET Framework)” which builds to a *.dll. There may be many other options, depending on what is installed with Visual Studio. Some of the other options for me are:
    * “Class Library (.NET Standard)”
    * “Class Library (.NET Core)”
    * “Class Library (Portable)”
    * “Class Library (Universal Windows)”
    * etc.

    Actually, some of these might work – but the .Net Framework Class Library definitely works. I had a similar issue with adding the WPFUI project.

  2. Hello All.. (just for info)

    Had The same Problem , with VS2017 if use .NET Standard for the Engine , The Engine submenu will be ‘Dependencies’ Instead Of Having ‘Properties And References’ As the sub menu
    Also end up with different results In the Build Action Options when add the images and try set there Properties. No ‘Resource’ option Only ‘Embedded Resource’ in the list

    Using The Class Libarry.NET FRAMEWORK ,, worked and all sub menu now match with yours

    1. Thank you. I’ve updated the lesson on creating the class library project. It’s frustrating that Microsoft decided to have several different types of class libraries, with different features unavailable in the different versions.

  3. Question:

    Why don’t we make the variables public?


    public string Name {get; set;}
    etc etc..

    I’ve read somewhere that making the variables public is wise when we want to access the information from other classes later on? I might be wrong but just a thought =)

  4. I’m curious as to why you chose to mention the decimal type as opposed to double. As far as I’m aware, decimal is really only used for financial calculations where you need a high precision and you’re willing to sacrify some performance, since the FPU is not involved in any way when using decimal.
    Am I missing something here?

    1. No particular reason. My personal preference is to use decimals as my default non-integer numeric datatype, to avoid floating-point precision problems. I’ll use doubles if I’m doing something that does a lot of calculations, like some of the engineering programs I’ve worked on. I think most programmers use doubles as their default non-integer numeric datatype, even when their program doesn’t have any performance concerns. Then, they end up with equality comparison problems.

  5. Great Tutorial and video Scott – WPF is a big leap but I guess it has to happen. Thanks for the lessons on Visual Studio too while building the program

  6. Hi Scott,
    A small question. One of the properties on your form was called “class”, yet, you named it “CharacterClass” in your program. Is that because the compiler might bark at the use of the word “class” or is it to avoid confusion while programming?

    1. Hi Patrick,

      It’s to avoid confusion. We should be safe to use “Class” for a property name, since it has different casing from the reserved word “class”. But, it’s close enough that it could be confusing. One rule I try to follow when writing code is to eliminate as much confusion as possible. That way, if I (or another programmer) look at the code in six months, it’s easier to understand what the code is doing.

  7. Hello Scott! Thanks very much for these tutorials (Old and New!) I finally understand where I’ve been going wrong with the others I’ve tried. I imagine that your tutorials will see a lot more visitations now that a virus id forcing physical distancing/Isolations!
    Good Luck and Health to you!

    1. Hi Thomas. You’re welcome! I hope people are taking advantage of the quarantine. I’m still doing contract programming, but working from home now. I hope you’re staying safe and healthy too!

  8. Thank you for the tutorials!! I did the old one some time ago, and learned a lot. I really enjoyed creating my own game, and now I am back for the new series.
    I wish there was a android tutorial like this, I would love to create an app game.

    1. You’re welcome, Riske! That’s great to hear.

      For Android tutorials, you might want to look for Unity/Xamarin tutorials. I think those will let you write in C# and output to Android.

Leave a Reply

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