Shared Project : An Impressive Feature of Visual Studio 2015 Preview

Overview

Visual Studio is the product of Microsoft that is the most productive IDE in the industry and recently Visual Studio 2015 Preview has been released that is the latest IDE. It was announced by Microsoft on November 12, 2014 at the day of Visual Studio Connect() event from New York, USA. If we want to watch these announcement, we can watch on the following link of Microsoft.

Connect

On the day of the announcement very many new features as well as improvements in existing features were introduced by Microsoft's professionals/speakers but in this article we will learn about only one feature, Shared Project that is one of the most impressive features of Visual Studio 2015 Preview.

Shared Project

Visual Studio 2015 Preview provides a shared project so we  can use this project in any other project, either console, Windows or web applications by simply using add project reference. This feature was not available with any previous version of Visual Studio. Let's see how to use it.

Note: Before the release of Visual Studio 2015 we often had to create a shared project and we would create it but the way of creating and using was quite different. But now we can create a shared project very easily. The following is the procedure to find and use shared projects of Visual Studio 2015 Preview. 

Step: I

Open a new project dialog and search shared in the search box available on the top-right of the new project dialog box. Now we can see the following dialog as in Figure 1. In Visual Studio 2015 Preview select "File" -> "New" -> "Project..." then we see the following dialog. Now type shared into the search box of this dialog.

Figure1

Step: II

Now select the Share Project Visual C# and provide the name MySharedProject for the project and click on the OK button. Our shared project has now been created. We now need to add some class to do some logic that will be used by the other projects in which we want to use it.

Step: III

For example, we will add a class with the name Employee.cs using the following procedure. Right-click on the project in Solution Explorer then selelct Add -> New Item -> and select a class file and name it Employee.cs.

AddClass

Step: IV

Now we can write the code in the Employee.cs file depending on our requirements but for the time we will write the following lines of code.

Employee.cs
  1. public class Employee  
  2. {  
  3.   //Properties  
  4.   public int EmployeeID { getset; }  
  5.   public string FirstName { getset; }    
  6.   public string LastName { getset; }  
  7.   public string Phone { getset; }  
  8.   public string Address { getset; }  
  9.   
  10.   //Methods  
  11.   public string GetEmployeeDetail()  
  12.   {  
  13.     EmployeeID = 247;  
  14.     FirstName = "Ved";  
  15.     LastName = "Prakasj";  
  16.     Phone = "9548587460";  
  17.     Address = "Janakpuri New Delhi, India";  
  18.       
  19.     return string.Concat("Name : ", FirstName, " ", LastName,  
  20.                          "\nEmp ID : ", EmployeeID, "\nAddress : ",  
  21.                          Address, "\nPhone : ", Phone);  
  22.   
  23.    }  
  24.   
  25. }  

Build the project. It will then be ready for use with any other application. Let's create a new Console, Windows Form and Web applications in which we will use this shared project. First do it with a Windows Forms application.

With Windows Form Application [Second Project]

Now we will create a new project as a Windows Forms application in which we will use this shared project. So let's use the following procedure to do it.

Step 1

To create a new Window Forms Application use the following procedure.

Right-click on the Project Salutation then seelct Add -> New Project then and select Windows Forms Application and name it WindowsFormApplication.

Step 2

Now add a reference of the MySharedProject into this Windows Forms application, to do it use the following procedure.

Right-click on the WindowsFormApplication's project and select Add Reference. You will then see the following dialog:

PReference

Now we have added the reference of the MySharedProject into this Windows Forms application. We can see this reference inside the references folder of the current project, it looks like the following:

InSideReferece

Step 3

Now open the code file of Form1 and use the following namespace: 
  1. using MySharedProject;  

 Step 4

Now add a button and a label onto Form1 and write the following line of code behind the button1_Click event.

 

  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.    Employee employee = new Employee();  
  4.    string result=employee.GetEmployeeDetail();  
  5.    lblDetails.Text = result;  
  6. }  

 Step 5

Now set this project (WindowsFormApplication) as the Startup Project and execute the application. To mark this project as the startup project right-click on the project WindowsFormApplication and select Set as StartUp Project.

Step 6

Now to see the output click on the button of running window.

   Output
FormOutput

 Note:

We will do the same procedure for a Console Application as well Web Application. We followed the same procedure for ConsoleApplication and got the following output that is same as the preceding because we are using the same shared project.

Output
Consoleutput

Summary 

In this article we learned how to create a Shared Project in Visual Studio 2015 Preview and we also saw how to use this project in another applications. Thanks!

Up Next
    Ebook Download
    View all
    Learn
    View all