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 Form” project (File -> New Project -> Windows Form Application). Make sure you are using the using the project templates listed under “Visual C#”.

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, in the Solution Explorer and add a new “Class Library” (or “Class Library (.NET Framework)“, if you are using Visual Studio 2017) project named “Engine” (Add… -> New Project… -> Class Library).

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 Express 2013 for Desktop

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

52 thoughts on “Lesson 02.2 – Building the solution for the game

  1. 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?

     

    1. 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.

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

  2. 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!

  3. 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.

    1. 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).

Leave a Reply

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