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