Press "Enter" to skip to content

Lesson 02.2 – Building the solution for the game

Lesson objectives

At the end of this lesson, you will know…

  • How to create a program (solution) in Visual Studio
  • How, and why, to have separate projects in your solution

 

How to build your first solution in Visual Studio

People generally use “program”, “application”, and “project” interchangeably. However, Visual Studio calls a program a “solution”.

A “solution” is broken into smaller pieces that Visual Studio calls “projects”. The solution must have at least one project.

In these lessons, I’ll use “program” to talk about the game, “solution” for all the files used to make the game, and “project” for the different groups of files within the solution.

 

Deciding how many projects the solution needs

When I create a solution, there are always at least two projects.

One is for the user interface (UI), the part of the program that reads and writes to the screen. The second project is for the “logic” of the program.

I do this is for a couple of reasons.

First, it makes the program easier to test.

We won’t do it in these tutorials, but it’s possible to write automated tests to make sure the program does what it’s supposed to do. If you have “logic” in the user interface, this is difficult to automate.

Second, sometimes you’ll want to use one of your projects in another program.

One example might be the code to read from, and write to, a database. That could be useful for any program you write that needs to access a database.

If the database code is in the UI project, you won’t be able to use it in another project, unless it needs to use the same screens from the UI project.

So you put that all that database access code into a separate project, and you can copy it to your next project that uses a database.

When we write the game, you’ll see how I decide what to put in the Engine project.

NOTE: For a large business program, you might have dozens of projects in a solution – which sometimes leads to problems with keeping track of everything.

 

Creating a solution with two projects

Here’s a video of how to create the solution for your game.

 

 


Link to video on YouTube

 

Step 1: Start Visual Studio Express 2013 for Desktop

 

Step 2: Create a new solution with a “Windows Forms” project (File -> New Project -> Windows Form Application). Make sure you are using the using the project templates listed under “Visual C#”.

NOTE (for Visual Studio 2017 users): You can create this project under Visual C# -> Windows Classic Desktop -> Windows Forms App (.NET Framework). The “Engine” project will be under Visual C# -> Windows Classic Desktop -> Class Library (.NET Framework)

A Windows Form application is a user interface project, one that will display the program on the screen, wait for the user’s input, and update the information on the screen.

We’ll name the program “SuperAdventure”, and save it in the C:\Projects directory.

Important note!If you give your UI project a different name, click here to see how to fix the namespace (so you won’t have problems in the rest of the lessons).

 

 

Step 3: Now we’ll create the “logic” project.

Right-click on the name of the solution “SuperAdventure”, in the Solution Explorer and select Add -> New Project…, to bring up the project selection screen.

For the project type, select Visual C# -> Windows Classic Desktop -> Class Library (.NET Framework). Name the project “Engine”. (See the image above)

NOTE: If you are using Visual Studio Community Edition 2015, you want the project type that just says “Class Library”, NOT “Class Library (Portable)” or “Class Library (Package)”

The project didn’t need to be named “Engine”. That’s just what I like to name it. However, use “Engine” when you create yours, so it will match what’s in the other lessons and videos.

 

Step 4: When you create a Class Library project, Visual Studio automatically adds a “Class1” class. You don’t need it, so right-click on it and select delete, to get rid of it.

 

Step 5: When you create a Windows Form project, Visual Studio creates a “Form1” screen by default.

You don’t need to rename it, but I always do. Right-click on it, select “Rename”, and rename it to SuperAdventure (be sure to leave the “.cs” on at the end).

The form name does not need to match the project name. That’s just what I chose to name it for this program.

 

Step 6: For the UI project to use the code in the Engine project, we need to connect them.

This is called “setting a reference”.

In the SuperAdventure project, right-click on References -> Add References… -> Solution (which will show you the other projects in the solution) -> Engine -> OK.

Now you can see that “Engine” is listed as a reference in SuperAdventure.

This means that the SuperAdventure project will be able to use the code in the Engine project.

 

Step 7: Now you can run the program.

Click on the “Start” button to see what the game looks like.

It’s not much yet, but you have to start somewhere.

NOTE: When you create the solution, Visual Studio says “New Project…”, when it really should say “New Solution…”, to use the correct term.

 

Summary

Now, everything is ready to start writing the program.

The solution has two projects – one for the UI, the other for the code to do all the calculations and logic for the game.

If you had any problems, or have any questions, please leave a comment below.

 

Next Lesson: Lesson 03.1 – Building the first screen

Previous lesson: Lesson 02.1 – Installing Visual Studio Community 2017

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

90 Comments

  1. Jason
    Jason August 5, 2016

    Hi Scott! I’m using Visual Studio Community 2015 and I’m having trouble adding the Engine class library.

    First of all, when I go to Add Item (“Add Project” is no longer an option) Class Library is not listed as a valid template.

    If I go to New Project then there are a few options: Class Library (Portable for iOS, Android, and Windows), Class Library, and Class Library (Portable).

    However, I’ve tried making each of these and I can’t seem to add them as a reference to the SuperAdventure project. If I choose Add Reference and go to browse, it’s by default searching for .dll, .tlb, .olb, .ocx., .exe, or .manifest files, none of which seem to be in any of the Engine class libraries I’ve tried creating. Any ideas?

     

    • Scott Lilly
      Scott Lilly August 5, 2016

      Hi Jason,

      When you add the Engine project, do you right-click on the solution file (in the Solution Explorer section), and see these menu options? This is where you would create the new project in the SuperAdventure solution.

      The project type for the Engine project should be “Class Library” – without the “Portable”, or “Portable for iOS”.

      When you add the reference, do you select the “Projects” section on the left side of the Reference Manager popup screen? That should show the projects in the solution. It should look like this:

      Please tell me if those do not fix the problem.

      • Jason
        Jason August 5, 2016

        Ah, I was right clicking on the project instead of the solution, stupid mistake! Thank you so much for the quick response.

  2. Light Ohana
    Light Ohana September 25, 2016

    Hi, Just wanted to Say Thanks For this Brief Tutorial, Really helps. Love the way you Describe Parts of the Solution as you talking to “Dummies”, Makes it all the more easier for ANYONE to learn from you. GOOD JOB!

    • Scott Lilly
      Scott Lilly September 25, 2016

      You’re welcome! I tried to make it so a beginner, without any experience, could follow the lessons.

  3. Chris Kelly
    Chris Kelly September 26, 2016

    When starting a new project I don’t have an option to choose location or solution name, only “Name:”. Also I’ve done this and when right clicking and trying to add class library, add project is not an option.

    • Scott Lilly
      Scott Lilly September 26, 2016

      Are you creating the solution by clicking on the “File” menu option (double-check with the video to make sure you are in the correct place)?

      If you are, can you take a screenshot, and upload it someplace for me to look at? Make sure it includes the Solution Explorer area of Visual Studio (the upper-right section).

  4. Markus
    Markus September 11, 2017

    Hey Scott, I’m having some trouble following this. You’re saying find form1 or delete class1 and I don’t see either. I’m not sure if I’m looking in the wrong place though, but was wondering if you could make some quick screen captions? Or maybe kind of describe where to find them. I’m using VS 2017 and used the .NET class framework option. Please let me know, and thank you for making this for noobs like me! lol

    • Scott Lilly
      Scott Lilly September 11, 2017

      When you have your solution open in Visual Studio, do you see the “Solution Explorer” section in the right part of the screen? That is where the files are. The picture below is a little blurry, because I got it from a screenshot of the video.

      If you do not see the Solution Explorer when your solution is loaded, go to Visual Studio’s menu and select “View” -> “Solution Explorer”. That should make it visible.

      Please tell me if you still do not see the files.

  5. Fighter Stakes
    Fighter Stakes October 30, 2017

    Hi there! thanks a bunch. I was wondering, if I’m running Visual Studio on Mac OS, what would be the equivalent to a windows forms app? I’m guessing: Forms App? Will I be able to follow through the whole tutorial? Cheers.

    • Scott Lilly
      Scott Lilly October 30, 2017

      I have never tried Visual Studio on the Mac. But, I expect there will be some differences. I don’t know what features are available in a Visual Studio for Mac Forms App, but that’s probably your best chance – unless you can create a VM (Virtual Machine) with a program like Parallels, and install Windows in the VM.

      • Fighter Stakes
        Fighter Stakes October 31, 2017

        Yup, I did a partition and installed Windows 10, now I have Visual Studio Community 2017, this tutorial was way too good to pass! So, my recommend: If you have a Mac and you want to follow this tutorial: Boot Camp Helper! Thanks Scott, I’m on my way, will let you know of my progress. I’m planning on creating a browser text-based rpg sports sim, something in the likes of simhorseracing.com I’m thinking this tutorial is a great place to start.

        P.S.- Another comment though, when you download Visual Studio Community 2017 make sure to install all the .NET packages or you won’t be able to find: Class Library. Thanks again.

        • Scott Lilly
          Scott Lilly October 31, 2017

          Cool! Thanks for sharing the tips for other Mac users who might want to do these lessons.

        • Kaimurai
          Kaimurai December 3, 2017

          Hey just wanted to let everyone know that the other option for Mac developers is to follow along with the WPF tutorial instead of the Windows Form tutorial. ASP.NET Core does not support Windows Forms (I imagine Microsoft will be phasing this out over the next few years) but it does support Web Forms Projects! To access this simply:
          1. Click on “New Solution”
          2. Scroll down on the template screen until you see “Other”
          3. Click on “.NET”
          4. Click on “ASP.NET Web Forms Projects” at the bottom
          5. Click “Next” and then follow the WPF tutorial on the remaining screens!

          • Scott Lilly
            Scott Lilly December 4, 2017

            Thanks for sharing. I saw the Mac version had XAML in web projects, but did not know if they could do everything we need. The game only uses basic XAML, so the WPF version of the source code could work (or require minor changes).

          • ianrna
            ianrna December 6, 2017

            When I try creating the project and saving it to C:/Projects it says I don’t have permission. Help?

          • Scott Lilly
            Scott Lilly December 7, 2017

            Did you try using the other slash “\”? If you did, and it still gives an error, can you take a screenshot with the error?

  6. Jennifer Rutherford
    Jennifer Rutherford December 12, 2017

    Hey, I can’t hear anything in the video, watching it from here and from YouTube. Is it supposed to be silent?

    • Scott Lilly
      Scott Lilly December 12, 2017

      There is no audio for most (maybe all) the videos for this course.

  7. Pendrake
    Pendrake December 19, 2017

    When you first take off in the tutorial explaining the type of file to create…you say to use windows forms. I’m using VS 2017 and there are several options under windows forms. Is there a particular one i should use and am I going to be able to complete this tutorial in VS 2017 without too many discrepancies?

    • Scott Lilly
      Scott Lilly December 20, 2017

      You will be able to use this with Visual Studio 2017. When you create the projects, choose “Visual C#” -> “Windows Classic Desktop” -> “Windows Forms App (.NET Framework)” for the SuperAdventure project, and “Class Library (.NET Framework)” for the Engine project.

      • Pendrake
        Pendrake December 20, 2017

        Thank you….i hadn’t downloaded the proper add on.

  8. Ryan Mull
    Ryan Mull May 25, 2018

    Hello,

    I have a question which may be silly. I have a MacBookPro and installed visual studio community. I don’t see an option for windows forms. Does anyone know if the Xamarin.Forms is the same thing or do i need to install another part of VB to get Windows Forms? Or can I not program for Windows Forms on the MAC?

    Thanks

    • Scott Lilly
      Scott Lilly May 25, 2018

      Hi Ryan,

      I don’t believe Windows Forms projects are available in the Mac version of .NET. If you can create a XAML UI project, you might be able to use the lessons from the WPF version of this course – but I don’t know if anyone has tried this, or if it really works.

      The only guaranteed way for it to work would be to use a Virtual Machine program – like Parallels, VMWare, or VirtualBox – to create a virtual Windows computer on your Mac.

  9. jesse
    jesse January 10, 2019

    Hi Scott.

    I have VS Community 2017 and I don’t see “Windows Classic Desktop”. I don’t see C# for “installed”, it’s only in the “online” tab. When I look down at the bottom of the pop-up screen, there’s a message that says “not finding what you’re looking for?” with a link to re-open visual studio installer, I’m assuming I need to install something specific to have “windows classic desktop” in my “installed” folder when creating a new project. Do you know what that might be or how I can do this?

    I have a screenshot if you want me to email you for a better understanding of what I’m talking about I can do that!

    Thanks,

    Jesse

    • Scott Lilly
      Scott Lilly January 11, 2019

      Hi Jesse,

      Are you running a Windows computer or a Mac? If you’re on Windows, run the Visual Studio Installer, and check that you have “.NET Desktop Development” checked (see the image below). If that doesn’t work, can you post a link to a screenshot?

  10. Jorin
    Jorin September 20, 2019

    I’m using VS 2019 Preview, which doesn’t have an option to add a new project to the solution.

    • Scott Lilly
      Scott Lilly September 22, 2019

      Hi Jorin,

      I’m using Visual Studio Community 2019, Version 16.2.5, and was able to add a new project. Make sure you right-click on the solution in Solution Explorer, and not the project. In the screenshot below, I already have other projects added to the solution, but the important thing is to make sure you try to add the project to the solution.

      Let me know if you still can’t add the project.

  11. Marcelo Cutin
    Marcelo Cutin May 30, 2020

    Hi, Scott.

    I’m using Visual Studio Community 2019 version 16.5.5 and I ‘m facing some troubles in two steps after creating the Engine project.

    When I try to delete Class1.cs file, I receive the following error message after confirming that I want it deleted:

    “This operation is not allowed while holding an active upgradeable read or write lock from an AsyncReaderWriterLock.”

    After clicking on ok button, the file seems to be deleted anyway because it is removed from the Solution Explorer.

    After that I tried to add the Engine project as a reference in SuperAdventure project. However, I receive a pretty much similar error message saying:

    “A reference to ‘Engine’ could not be added. This operation is not allowed while holding an active upgradeable read or write lock from an AsyncReaderWriterLock.”

    After clicking in ok button, Engine project is not listed among the references listed in SuperAdventure project.

    What should I do? I appreciate any help. Thank you!

    • Scott Lilly
      Scott Lilly May 31, 2020

      Hi Marcelo,

      I have not seen that error before. Maybe run the Visual Studio Installer and update to version 16.6.0, then reboot your computer. Let me know if that does not fix the problem.

      • Josh
        Josh July 10, 2020

        So I struggled with this same issue for some time and after circumventing it and scouring the internet then deleting Class 1 last, I think I figured it out?

        I believe that since deleting the class is the first step, Visual studio’s latest versions can not connect a reference with an inoperable class. Something it appears the 2013 version used in the tutorial does no problem.

        • Scott Lilly
          Scott Lilly July 10, 2020

          I have seen some strange behavior in Visual Studio 2019 – mostly (I think) because I have ReSharper installed.

          I’d be interested in seeing your solution, to track down the root problem. Can you upload your solution (including the directories under it, and all the files in those directories) to GitHub or Dropbox, so I can look at it?

          Thanks!

  12. ???
    ??? January 14, 2021

    Hello, I am using Visual Studio 19, and i cant get the reference to “engine” to work.

    • Scott Lilly
      Scott Lilly January 15, 2021

      That usually happens if the project types are not .NET Framework. When you created the projects, if you picked .NET Core, or .NET Standard, for one of the projects you might not be able to set the reference.

      If that is not the source of the problem, can you upload your solution (including the directories under it, and all the files in those directories) to GitHub or Dropbox, so I can look at it?

  13. Ashley Mahony
    Ashley Mahony February 11, 2021

    Love your guide! If you do not have Windows Class Desktop as an option due to Visual Studio being the latest version, is there another equivalent for the Engine?

    • Scott Lilly
      Scott Lilly February 11, 2021

      Thanks!

      I made a video, since that might be an easier way to show how to find the projects. Let me know if this doesn’t work, or if you have any other questions.

      https://youtu.be/dbMQiB7UO1g

  14. Henry
    Henry August 9, 2021

    Hey Guys!
    First, thank you so much Scott for this tutorial. It is literally like finding gold as a newbie c# writer. Second for anyone on VS 2019 who has run into the referencing issue after deleting Class 1 from their engine, the solution I found was to match the .Net versions of the two projects. Otherwise the engine cannot be referenced because (I assume) the different versions of .Net are being read as versions of Core or Standard. This was at least what I took away from the error messages. (After about 20 attempts) I finally got it working by using version 4.0 of the .Net framework. Anyways I hope this helps and once again thank you so much!

    • Scott Lilly
      Scott Lilly August 9, 2021

      Thanks for sharing that, Henry. In the seven years since I wrote these lessons, Microsoft has changed a lot (.NET Core, .NET 5, new versions of Visual Studio). Most of the problems people encounter are from using non-.NET Framework projects.

  15. Bastian Fuentes
    Bastian Fuentes November 2, 2021

    When I create the Forms, it starts with 0 projects in it, and can’t reference. I’m using VS 2019, maybe I am dumb?

    • Scott Lilly
      Scott Lilly November 2, 2021

      Hi Bastian,

      When you create the solution, start by selecting “Create a new project”
      Visual Studio 2019 Create New Project

      Then, enter “framework” in the search box and select “C#” projects. For the first SuperAdventure (UI) project, select “Windows Forms App (.NET Framework)”. Later, when you create the Engine project, select “Class Library (.NET Framework)”.
      Visual Studio 2019 Select project type

      Please let me know if that doesn’t work, or if you have any other questions.

      • Bastian Fuentes
        Bastian Fuentes November 2, 2021

        Ah, I was using the Windows Forms App without the (.NET), problem solved, thanks

  16. Ainsley
    Ainsley December 1, 2021

    I am running in Visual Studio Community 2019. (16.11.2)
    -Windows Form App (.NET Framework)
    -Class Library
    Dependencies: Selected Enginer

    Visual Studio cannot start debugging because the debug target C:\Users/username
    source\repos\SuperAdventure.exe’ is missing. Please build the project and retry, or set the OutputPath and AssemblyName properties appropriately to point at the correct location for target assembly.

    Believe it’s confused on .NET Framework, but many of the Installation Details aren’t listed anymore, i.e.: .NET Core 2.1 development tools, .NET Core 2.0 development tools. Would love some assistance!

    • Scott Lilly
      Scott Lilly December 1, 2021

      Hi Ainsley,

      Can you upload your solution (including the directories under it, and all the files in those directories) to GitHub, Dropbox, or some other file-sharing location so I can look at it?

Leave a Reply

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