Creating a C/C Sharp (C#) Project in Visual Studio Express 2013

Pinterest icon

Visual studio tutorial front page

Visual Studio is an integrated development environment from Microsoft for software development on the Windows platform using C/C++,C# and Visual Basic.NET languages.Visual Studio is a commercial software aimed at large organizations or professional programmers.

Microsoft also provides stripped down versions of Visual Studio called the Visual Studio Express Editions geared towards hobbyists and students, rather than professional software developers.

Express editions used to be geared towards single languages like Visual C# Express or Visual Basic Express, As of 2012 Microsoft have stopped the individual language editions and have started providing an overall solution type supporting multiple languages in a single IDE.

In this tutorial, I am using the Visual Studio 2013 for Windows Desktop Edition which can be freely downloaded from the Visual Studio site. The download is available as an online installer or an ISO image.Microsoft have released a new Community edition of Visual studio also,You can check all the the available downloads here.

downloading visual studio 2013

After the software is installed, VS Express 2013 may ask you to register an account with Microsoft. Just create a free account with Microsoft and register the software.In case you didnt want to use Visual studio,SharpDevelop is a great opensource alternative.

Click on the desktop icon to launch the software.
You will be greeted by a Start page, Under the “Start” section you can see options for creating a “New Project....”,
and for opening an existing project “Open Project..” as shown below

Visual studio start page

The same options can be accessed using the “File” Menu.

creating a new project in visual studio

Creating a New Project
Visual Studio Express 2013 can create a project in three languages C/C++,C#,VB. Net.
To Create a new project Click on the “New Project....” either from the Start page or from file menu.
On clicking “New Project....” a Project wizard is started and you are presented with the option to select the language for your project.

Creating a New Project in Visual Studio Express 2013

On the side pane under “Templates” you can see the 3 languages that are available with the IDE. Here we are going to develop in good old C ,so select “Visual C++”.
On the right pane we will see the various types of projects such as a Console program, a Win32 gui program etc that are available for a Visual C++ project. If We Click on a “Win32 Project” the IDE will create a set of boiler plate code for creating a gui.
Here we are going to create an “Empty Project”.

So Click on the Empty Project and give it a name and select the Directory to store your files.
Lets give the project name as “SerialComm” and press “OK”.

You will be greeted by a “Solution Explorer” Tab containing Folder shaped icons named “Header Files”,”Resource Files” and “Source Files”.

solution explorer in visual studio

Please note that these are not folders and are called “Filters” in Visual Studio. If you open the Project folder you will not find any of these folders. They are used to group similar types of files. You can add your own Custom filters by right clicking and selecting “Add → New Filter” option.

Adding filters in visual studio

You can give them Custom Names like “MyCodes”.Sub Filters can be created under existing filters like “Source Files”,”Resource Files“ etc. Please note that deleting the Filter will remove all the contents under it.

renaming a filter in visual studio 2013

Adding Files to the Project

For our SerialComm project,
We can either add the downloaded C Source codes or you can type the C code by hand.

If you don’t want to type the whole C code, You can right click on the “Source Files” and go to “Add → Existing Item...” and then navigate to your source files and click add.

add existing item to visual studio project

Now the file that you have selected will be displayed under the " Source Files " Filter.

Now on the other hand if you decided to Create a New File and type the code by hand,
Right click on the “Source Files” and go to “Add → New Item...”. and the“Add New Item “window will pop up.

Select “C++ File”.At the bottom of the window, rename the file from “Source.cpp” to Serial.c (or any name of your choice with .C extension) as we are programming in C.You can also select the file location of your choice to store the c files.

Removing Files from Project

Now if we want to remove the file from the project. You can right click on the code file and choose either “Exclude From Project” or “Remove” option.

removing the files from a visual studio project

Exclude From Project” will remove the source files from the project but will not delete itfrom the file system. The source file will still be available in the project directory.

Remove” on the other hand will permanently delete the source files from the file system,fortunately the system will ask twice before permanently deleting the file.

 

Compiling the code

After you have added the source file to your project,the IDE will position them in a tabbed format along with the “Solution Explorer” like this

default tabbed view of the source files

Now if you don’t like the tabbed view and prefer the separate pane view, Drag the code tab here “USB2SERIAL_RS485_Write_W32.c” to the left so that “Solution Explorer” is on the right. Now Right click on the code tab and select “New Vertical Tab Group”.

setting the tab order in visual studio 2013

So that the layout looks like this (below)

new tab layout

Compiling and Building

After you have added the source file to the project you can build it to see if it runs properly. All the build options are grouped together under the "BUILD" menu.To build your project you can press F7 or click on the “Build Solution”.

building a project in visual stuio 2013

The IDE will compile and link your code file to an executable, if all goes well you can see the build succeeded message in the output window.

output window showing a successful compilation in visual studio

In case there are any errors they will be listed in the output window. Usually intellisense will catch any errors before the build and mark it with red wavy lines.

Visual Studio displaying errors

After you have build your project you can debug/run it by pressing F5 or going to the “Debug” Menu and clicking “Start Debugging”.

debugging a c Project in visual studio

The other debugging commands like Stepping through the Code, toggling the breakpoints are also conveniently grouped together inside the Debug menu.
Alternatively you can launch a debug session by directly clicking on the “Local Windows Debugger” Button on the Menu bar. There is also an option to select between Debug mode and Release mode (no debugging info).

 

Creating a C# Project in Visual Studio 2013
Creating a C# project is similar to the C Project mentioned above. The difference is that while you are selecting the language for your project ,Click on the C# link and select the appropriate project type.

rating a new csharp (C#) project in visual studio 2013

Since we are creating a project for communicating with hardware it is better to go for a console application instead of an empty project. Problem is that when we are creating an empty project for C#, the IDE is not allowing to access the “System.IO.Ports”namespace.

Lets name your new C# project as "serial" and click OK to create the new project.After the project is created you can start adding code files to it,You can either add preexisting files or create new ones.

Before adding the files, let's create a folder to organise our code files.For simple projects it won't make much difference but when you are building a large program it would help you to organise your work.In the first section of the tutorial (C section), visual studio is able to create virtual folders called Filters to organise the code files (c files).In the C# section of Visual Studio there is no concept of Filters,instead we can add actual Folders to your Solution Explorer.

Click on "Serial" on your Solution Explorer and got to "Add  New Folder" to add a folder to your Project tree.

adding a new folder to visual studio

After you have added the folder let's rename it into "MyCSharpCode" which will hold our code files.Please note that unlike the Filters ,the Folders that you add actually exist on the File system.If you open your Project in windows explorer you can see the "MyCSharpCode" folder inside your project directory.

There are two ways to add codes to your project ,

You can create a new file using "Add New Item... " .If you select that, a new window will pop up showing the items available to you like Class file,data base,interface file etc.Select CodeFile option.

After you click OK the CodeFile.cs can be seen under the "MyCSharpCode" Folder.You can rename the CodeFile.cs according to your choice. 

The second way is to add existing files using "Add → Existing Item... " and navigate to the location containing your code files .Here I am using the C# files from the Serial Communication tutorial

After the codefiles are added you can run and debug the program by pressing F5.

 

 

Tags: