Lesson 04.1: Creating the Location class

In this lesson, we will create a class to hold the Location information (including a graphic file to display).


NOTE: If you created the projects with .NET Core, you’ll have a problem with the images. They need to be referenced another way. A reader gave these instructions:

If you use .NET core the ImageName property should be “pack://application:,,,/Engine;component/Images/Locations/Home.png”. The images will disappear from the solution explorer when you change their build action to resource, just check your .csproj file and you will see references to them there.

You also have to change your Engine.csproj to be
<Project Sdk=”Microsoft.NET.Sdk.WindowsDesktop”>
instead of
<Project Sdk=”Microsoft.NET.Sdk”> at the top.




To add an image to your project:

  • Create the image in your preferred graphics program.
  • Add the file to the project, as an existing item.
  • Set the images’ “Build Action” property to “Resource”
  • When displaying the file, you will need to include the assembly (project) name where the file is located, along with the path (folders) to the image file.
    • For this project, it will be “/Engine;component/Image/Locations/Home.png” (for the Home location image).


Image Files

All image files, in a zip file

Or, download the individual images (right-click, and save to your disk):

Source Code


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

namespace Engine.Models
    public class Location
        public int XCoordinate { get; set; }
        public int YCoordinate { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string ImageName { get; set; }



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

namespace Engine.ViewModels
    public class GameSession
        public Player CurrentPlayer { get; set; }
        public Location CurrentLocation { get; set; }

        public GameSession()
            CurrentPlayer = new Player();
            CurrentPlayer.Name = "Scott";
            CurrentPlayer.CharacterClass = "Fighter";
            CurrentPlayer.HitPoints = 10;
            CurrentPlayer.Gold = 1000000;
            CurrentPlayer.ExperiencePoints = 0;
            CurrentPlayer.Level = 1;

            CurrentLocation = new Location();
            CurrentLocation.Name = "Home";
            CurrentLocation.XCoordinate = 0;
            CurrentLocation.YCoordinate = -1;
            CurrentLocation.Description = "This is your house";
            CurrentLocation.ImageName = "/Engine;component/Images/Locations/Home.png";



&lt;Window x:Class="WPFUI.MainWindow"
        Title="Scott's Awesome Game" Height="768" Width="1024"&gt;
            &lt;RowDefinition Height="Auto"/&gt;
            &lt;RowDefinition Height="*"/&gt;
            &lt;RowDefinition Height="225"/&gt;

            &lt;ColumnDefinition Width="250"/&gt;
            &lt;ColumnDefinition Width="*"/&gt;

        &lt;Label Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Content="Menu" Background="AliceBlue"/&gt;

        &lt;Grid Grid.Row="1" Grid.Column="0" Background="Aquamarine"&gt;
                &lt;RowDefinition Height="Auto"/&gt;
                &lt;RowDefinition Height="Auto"/&gt;
                &lt;RowDefinition Height="Auto"/&gt;
                &lt;RowDefinition Height="Auto"/&gt;
                &lt;RowDefinition Height="Auto"/&gt;
                &lt;RowDefinition Height="Auto"/&gt;
                &lt;RowDefinition Height="Auto"/&gt;
                &lt;ColumnDefinition Width="Auto"/&gt;
                &lt;ColumnDefinition Width="Auto"/&gt;
            &lt;Label Grid.Row="0" Grid.Column="0" Content="Name:"/&gt;
            &lt;Label Grid.Row="0" Grid.Column="1" Content="{Binding CurrentPlayer.Name}"/&gt;
            &lt;Label Grid.Row="1" Grid.Column="0" Content="Class:"/&gt;
            &lt;Label Grid.Row="1" Grid.Column="1" Content="{Binding CurrentPlayer.CharacterClass}"/&gt;
            &lt;Label Grid.Row="2" Grid.Column="0" Content="Hit points:"/&gt;
            &lt;Label Grid.Row="2" Grid.Column="1" Content="{Binding CurrentPlayer.HitPoints}"/&gt;
            &lt;Label Grid.Row="3" Grid.Column="0" Content="Gold:"/&gt;
            &lt;Label Grid.Row="3" Grid.Column="1" Content="{Binding CurrentPlayer.Gold}"/&gt;
            &lt;Label Grid.Row="4" Grid.Column="0" Content="XP:"/&gt;
            &lt;Label Grid.Row="4" Grid.Column="1" Content="{Binding CurrentPlayer.ExperiencePoints}"/&gt;
            &lt;Label Grid.Row="5" Grid.Column="0" Content="Level:"/&gt;
            &lt;Label Grid.Row="5" Grid.Column="1" Content="{Binding CurrentPlayer.Level}"/&gt;
            &lt;Button Grid.Row="6" Grid.Column="1" Content="Add XP" Click="ButtonBase_OnClick"&gt;&lt;/Button&gt;

        &lt;Grid Grid.Row="1" Grid.Column="1"
                &lt;RowDefinition Height="*"/&gt;
                &lt;RowDefinition Height="*"/&gt;
                &lt;ColumnDefinition Width="3*"/&gt;
                &lt;ColumnDefinition Width="2*"/&gt;
            &lt;Border Grid.Row="0" Grid.Column="1"
                &lt;Grid Margin="3"&gt;
                        &lt;RowDefinition Height="Auto"/&gt;
                        &lt;RowDefinition Height="*"/&gt;
                        &lt;RowDefinition Height="Auto"/&gt;
                    &lt;TextBlock Grid.Row="0"
                               Text="{Binding CurrentLocation.Name}"/&gt;
                    &lt;Image Grid.Row="1"
                           Source="{Binding CurrentLocation.ImageName}"/&gt;
                    &lt;TextBlock Grid.Row="2"
                               Text="{Binding CurrentLocation.Description}"/&gt;
        &lt;Label Grid.Row="2" Grid.Column="0" Content="Inventory/Quests" Background="BurlyWood"/&gt;
        &lt;Label Grid.Row="2" Grid.Column="1" Content="Combat/Movement Controls" Background="Lavender"/&gt;



Return to main page

38 thoughts on “Lesson 04.1: Creating the Location class

  1. Hi Scott,

    Just a comment – when adding the images and changing the Build Action from Content to Resource, you can select all the images in the Solution Explorer at once and change them all at once, rather than clicking each one and changing it individually.

    To select multiple items:
    1> click on the first one, and hold Shift down and click on the last one, to highlight all the items
    2> Also, individual items can be added or removed from the highlighted list by holding down Ctrl and clicking.
    3> Once the desired list of all items is selected, in the properties window, change Build Action just like you would for a single item, but it will change it for all highlighted items.

    I think this is good to point out because it can be applied to any objects in the solution that need to have their properties updated to the same thing.

    1. Yes. This is especially useful when working on Windows Forms programs, when you want to change some properties for many textboxes/labels/buttons. It makes setting height/width/alignment/etc. much faster than individually clicking on each one.

  2. Hi Scott,

    Thanks for these tutorials!

    Can you explain why the “;component” is needed after “/Engine” in the Image path?

    1. You’re welcome, Mike.

      If you want one project (WPFUI) to use a resource (image) from another project (Engine), you need to include “;component”. If the resource is in the same project/assembly, you do not need to include “;component”.

      There is some more information on this at: Microsoft documentation

  3. Hello Scott,

    Thanks for this awesome tutorial. When I tried to add the build option “Resource” on the images it wasn’t available. I found out that it was because it was in the Engine project. Once I moved it into the WPFUI project the buildAction option for “Resource” became available. I am guessing it has to do with the references or type of project my Engine project is. I noticed there were two types of class libraries to choose from in visual studio 2017 the .net framework version and the .net standard version. I choose the standard. If you know why the buildaction “resource” wasn’t available please let me know. I tried stackoverflow but came up with nothing


    1. Hello Emmanuel,

      I haven’t worked with .NET standard projects, but Microsoft has been trying to make .NET usable in different environments (Mac, Linux, etc.). However, some functions are not available in all environments – or, at least not available right now. This is especially true with .NET Core. There is some more information on this Stack Overflow question. Although, the answer might be more confusing than the question. I haven’t found anything that specifically shows why resource wouldn’t be allowed in a .NET Standard Class Library.

      In my copy of the solution, using Visual Studio Community 2017, the Engine project is a “Class Library (.NET Framework)” under Visual C# -> Windows Classic Desktop, and it allows adding an image as a resource.

  4. Hi Scott,

    Thank you for making these great tutorials, I am learning a lot about C# and able to learn how to make games from scratch which is great.
    I have a couple of questions I hope you don`t mind and Sorry if these questions has been asked before.

    I noticed in the last couple of lessons it says how many references there is above public/private etc, I was wondering how you set that up?

    I was also wondering if their is a simple way to add music/sound effects?

    And will you be adding more lessons to this tutorial like the JRPG graphics etc?

    Thanks again for these great tutorials,

    1. You’re welcome, Karl!

      For the references, that might be ReSharper (a Visual Studio plugin I use). It might also be part of Visual Studio. Visual Studio is always adding new features that are in ReSharper.

      You can add audio, but I have not done that in any programs (other than speech in a C# program like Jarvis).

      I do plan to add JRPG graphics. I haven’t added any new lessons in a few months. I started a new job, have been working on some side projects for clients, and am a little burnt out (sometimes I need a break).

  5. Hello! Thanks for the great article!

    For some reason I’m having trouble with the image paths. I’m using Visual Studio 2019 and for some reason the “/Engine;component/Image/Locations/Home.png” (or adjust for whichever filename we’re using) path doesn’t work for me. The only way I can get the image to appear is if I copy and paste the full path from File Explorer, starting with the drive and going all the way to the image. Is there an easier way to find the image path for the project? I’ve looked in the documentation but haven’t found it yet.

    1. You’re welcome!

      I haven’t installed Visual Studio 2019 yet, but will see if I can figure out what the problem is. For now, you can use the full file path. In a future lesson, we start moving the game data from resources to dynamically-loaded values from an XML file.

    2. Thank you for the comment. I am working on VS2019 and I wasn’t able to see the picture on the output when using “/Engine;component/Image/Locations/Home.png”. I was banging my head against wall, checking all the code I have written. Now it shows the picture after coping the full path for the file as you said.

  6. Hi Scott, thanks a lot for the tutorials.

    Am having same issue with paths (Win 10, VS 2017 community)
    Am unable to add PNGs as resource, but using full path as suggested by V.H. works.

  7. Hi Scott, thank you for the tutorials!

    I have a little problem when I need to change Build Action on the images there is no resource option. Maybe you can help me.

        1. The problem is that the Engine project is a Class Library built with “.NET Standard”, and it needs to be a Class Library built with “.NET Framework”.

          There are two things you can do.
          1. Delete the solution, build a new one, and create the Engine project with .NET Framework.
          2. I found an older version of the solution that uses .NET Framework and copied it to https://www.dropbox.com/s/r4wj96pzxaq2l67/Rytis.zip?dl=0. However, that version is the code after lesson 04.3. So, you can read the next two lessons, but the changes are already made in those files.

          Let me know if you have any questions or problems.

          1. Heya Scott!

            I made the same mistake as others, created a .NET standard.

            There is a quicker fix to this solution.

            1. Right Click the Engine Project Class.
            2. Choose “Edit Project File”.
            3. Change the line standardnet2.0 to

            net462 = .NET framework version 4.6.2, change to the same version as the solution.
            I used 4.7.2 so my line is net472.

            4. Save
            5. Build Solution
            6. Run the project

            For me the images worked like a charm afterwards.

  8. Hello I am having an issue with the home image not showing at all

    I even did copy and paste of your code to see if it was my coding and still no images (I made sure they were set to resource)

    1. Hi Jesse,

      The problem was that the Engine project was set to use “.NET Core 2.0”, and it needed to be “Class Library (.NET Framework)”. .NET Core does not have all the features of .NET Framework. I created an Engine project in .NET Framework, moved your files into it, and removed the original Engine project. You can download the updated solution files at: https://www.dropbox.com/sh/6uowj5rf9949ep0/AACYlgAU47boGdBdiK7bTu_aa?dl=0

      Visual Studio has several different versions of .NET (Framework, Core, Standard). You have to be careful when adding a project, because it’s easy to pick a different version – which might cause problems when you build you progrma.

      Let me know if you have any problems with the updated code, or any other questions.

      1. Thank you very much!

        I must of confused it thinking they may of made a new framework with legacy features built into it.

        It’s completely working now, images and all.

        As far as questions
        What is core for? it seems to have less features if I’m reading that right

        1. You’re welcome.

          .NET Core was built to work on Windows, Mac, and Linux computers – so people can create .NET Core programs that run on all of those operating systems. So, .NET Core only includes features that can work on all those operating systems, and it’s going to take a while until Microsoft adds all the features from .NET Framework into .NET Core.

          1. That makes sense. I hope it catches up. would be cool to have that kind of ability with multiple platforms.

            I guess I have plenty to learn even when I learn within a specific language haha.

      2. I just wanted to add my 2 cents and say that his was the solution to this issue for me. I started with a Standard 2.0 library, but could not get the Binding to load, while a direct path in the Image element worked fine.

        I re-created the library in .NET Framework and it worked. Just a weird bit of incompatibility.

        Thanks for all your work!

        1. You’re welcome, Jakob.

          I recently started working at a new client and am using .NET Core – which Microsoft said if the future of .NET. I might look at what it would take to convert this project to .NET Core.

  9. Has anyone deduced how to get the image to appear in dotnet core yet?

    I have it working in framework, but wanted to go back through this with 3.1’s release, but can’t figure it out, or find any documentation on it. I know the features aren’t 1:1 core to framework, but surely core can reference a project file.


    The image does appear when using the full path, e.g. “@C:\Users\Stephen\Source\simpleRPG\simpleRPG_Engine\Images\Locations\tempHome.png”, so I’m certain the binding/etc. is fine, but the relative path “”//simpleRPG_Engine;component//Images//Locations//tempHome.png”” doesn’t show anything…but no crash or warning to read, either.  It appears in the .csproj file as an <ItemGroup>, so I no issue there.  It’s confounding me.

  10. Dear Scott, thank you for this course. Though I was a bit angry in the last episode, where I did not understood the INotify…Stuff directly, it only encouraged me to do some research on my own, which is good. I am so glad, you created this course, as there is just so much scam stuff on youtube and the internet that “teaches” a language just by showing its features in the console like “writeline” “a+b=f” and so on. Just a big thank you from germany. And though I am russian, I will recomend your course on the russian “facebiik (vk.com), so that other newbies can learn more and more with your help. It is just a common general lack, that there’s so much need beyond these “lessons for beginners” to learn a language by step by step examples. Again a Huge thank you. I’m really getting into it (an yes – my life depends on it, cause I lost my previous job in a call center). Please keep it up!

    1. You’re welcome, Chris! It’s difficult to write the lessons and decide how much to include, and how much to leave out. Fortunately, you were driven to do more research. That’s an important thing in programming. I have been programming for almost 40 years, and I still need to learn and research new things every week. For me, that is one of the best things about programming – you always get to exercise your brain.

  11. Hi Scott,

    over the last few months I’ve been (attempting/struggling) to learn C#.
    But since stumbling upon your website, through a random comment on stack overflow, I’ve learnt a lot, i think its the way you explain or the clarity in your pronunciation. Thank you for sharing your knowledge!

    anyway i’m thinking of going to University to study game development/programming, would you suggest university or to keep rummaging through website to website till you’ve cracked it?
    (the problem being, i’m a chef and its all i have ever done, so obtaining real work experience seems far fetched)

    1. You’re welcome, Jamie!

      It’s been a long time since my first job, but I got it because I knew a software consultant who needed a programmer, and he knew I could write small programs. So, networking with other programmers (or businesses that need programmer) will probably be the best way to find a job. But, they will want to see some proof you know how to program – at least the basics, if you are going to start as a junior programmer. A degree is a good way to show that, but you can also do that by having source code available on GitHub.

      If I was starting out again today, I’d probably build a good portfolio of personal projects on GitHub (making the code look as professional as possible, like including logging and error-handling). It could be something simple that might be useful in your current job, like a program to let cooks prioritize when they want to work. The program could then figure out who to schedule, based on the work needs and the priorities. You might never use the program, but it would be great to be able to demonstrate a program that solves a real business need.

      I’d also go to local tech meetups and try to meet as many people as possible – and let them know I’m looking for a junior/intern position. If you can go to University in your spare time (or have enough money saved to go full-time), that should help. You’ll learn a lot of things you might not from tutorials on websites and courses and Pluralsight/Udemy/etc. And definitely attend any career fairs the University has. The more “proof” you have that you can program (degrees, open-source projects, etc.), the easier it will be to get that first job.

      And remember to keep up those habits as you grow in your career. It’s usually easy for me to get a new project/job because I can show people this site, my GitHub repository, YouTube videos I created, etc. Plus, I’ve developed a good network of people who have seen the projects I’ve created when I worked with them.

      Good luck!

  12. Hi Scott,

    Love your tutorial in this course, which is easy to understand and easy to find a mistake in coding. However, in this lesson, it is hard to find the error, when I try to add the image into the program because the is no error showing. I have checked the source code with my code, but still haven’t found the problem. Can you have a look to mine code?

    Link: https://www.dropbox.com/s/n7qy4wevrt0pzd8/SOSCSRPG.rar?dl=0

    Thank you !!!

    1. Thanks Truong!

      I found a few problems in the code. I updated the solution and posted the files here: https://www.dropbox.com/sh/m2agpteqjo0pxim/AAC_-LsOwTYqM_sTnhSvb9C1a?dl=0

      – The Engine project was a “NET Standard 2.0” class library. It needs to be a “.NET Framework” class library project. There isn’t an easy way to convert the project, so I just created a new Engine project and manually added your classes and images into it.
      – This lets you set the properties of the images to “Content” “Copy always”, to ensure the location images are always copied to a sub-directory of the game program.
      – To display the location image files in the WPF window, I added a new custom converter to the WPFUI project – FileToBitmapConverter. Then, I changed lines 86-87 of MainWindow.xaml to use this converter.

      Let me know if you have any trouble running these changes, or have any questions about them.

  13. Hello, and thanks for making this tutorial, which I think is one of the best I’ve seen, if not the best. No other course/tutorial I’ve seen/taken, adresses the project setup with projects and folders like you do here, and I think that this is essential for getting the bigger picture that might enable one to create new programs with a good structure.

    I have one question so far. When changing the illustration properties to “resource” you saty the files becomes part of the assembly. Could you clearify what exactly an assembly is? I have an interest in this since the the default access modifier makes the object accessible to the assembly right? So if we cannot see between projects (we made classes public), how does an assembly work, and what is usually included?

    1. You’re welcome, Bjorn!

      Assemblies are DLL and EXE files. Usually, the main project in a solution (the WPF, Windows Form, or Command Line project) compiles to an EXE file when you build the solution. Class Library projects compile to DLL files. The assemblies contain the projects’ compiled classes, but can include other files if they are embedded into the assembly when you compile the project.

      Unless you are using some tricks, like with Reflection, an assembly can only use a class from a different assembly if the class is public. This is why we need to make the model classes (in the Engine project/DLL/assembly) and properties public – so they can be seen in the WPFUI project’s assembly.

      There is some more information about .NET assemblies here: https://docs.microsoft.com/en-us/dotnet/standard/assembly/

      Let me know if you have other questions.

Leave a Reply

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