Search This Blog

Saturday, October 15, 2011

C# 4.0 – Ch:02:F – Adding Projects to Solution


If you like this article, please click on +1 button and share with your friends.

Click here for the Video Tutorial of this article.
visit www.iGnani.com


...continued from article "C# 4.0 – Ch:02:E – Projects & Solutions : Continued

In the previous article, I explained while speaking about Solutions, I had mentioned that “a Solution is the set of all the projects that make up a particular software package”. Not only the projects that belong to a software package, but you can have projects even though they do not belong to the project.

In this article, let us look at adding additional projects to a Solution. Just to show you that we can add a project even though they do not belong to the project, let us use a “Windows Forms” project to our Demo1 solution.

Adding Projects to Solution

There are three ways to add a new project into the solution:

  • You can select  Add -> New Project from the File Menu.

    AddNewProject1[2]
  • You can right click on the Solution in the solution explorer and from the context menu that shows up, select Add -> New Project

    AddNewProject2[2]

When we try any of the above two methods, it will display the familiar Add New Project dialog box as shown below.

AddNewProjectDialog1[5]

If you notice the Add New Project window carefully, you can observer that we don’t have any option to select or specify a solution name as it was provided to use when we created the Demo1 project. Also you can see that the new project is being created in the same location as that of the Demo1 project.

  • Another way to add a new project to the existing solution is by selecting New –> Project from the File menu.

    AddNewProject3[2]

With this approach, the Add New Project dialog box is similar to what we saw while creating the Demo1 project. Check it out below.

AddNewProjectDialog2[7]

If you observe the Add New Project window above, we have two more options available namely “Solution” & “Solution Name”. The Solution name option is now presented as a drop down list and we have selected the value “Add to solution”, which disables the “Solution Name” field.

You are free to name the project anything as appropriate and the project can also be of any type. For this demo, I have selected the “Windows Forms Application”.

In accordance with the Language interoperability feature of .Net Framework, the new project does not need to be a C# project. It is perfectly okay to add a C# Project, a C++ Project, a Visual Basic Project or any other Project using any other language, unless that Language is supported by Visual Studio and has a compiler that can compile it into the IL code.

However, since this tutorial is about C# programming, we will stick with C#.

I will name this “Windows Forms Application” project as WinFormAppDemo. Next click on the OK button and this will add the “Windows Forms Application” project into the existing solution. You can see the new project in the Solution Explorer.

Now that we have another project in our solution, the name “Demo1” that we provided earlier to our solution is not really an appropriate name for the solution anymore. To change the name, in the Solution Explorer window, right click on the Solution name and select Rename from the context menu. Let us rename it from “Demo1” to “DemoSolution”. The Solution Explorer window should now look similar to this.

SolutionExplorer2[2]

You can now see that Visual Studio has added a new Windows Forms Application project into the solution and based on the project template that we have selected, it would have added references to appropriate namespaces.

As we changed the name of the solution from “Demo1” to “DemoSolution”, if you require to, Solution Explorer is the best place to rename. When we rename from the Solution Explorer, Visual Studio will automatically update any references to the file in all the necessary places, which is not the case if you rename the using Windows Explorer. By renaming from the Windows Explorer, you will need to manually edit and update the file references in all the necessary places.

Setting the Startup Project

If you remember our earlier chapters, we executed the Demo1 project simply by pressing the F5 key on the keyboard or by selecting the “Debug –> Start Debugging / Start Without Debugging” options. But now we have two projects.

Remember that if you have more than one project in a solution, only one of them can be executed at a time. When you compile the solution, all the projects (except configured otherwise) in the solution will be compiled. However, you must tell the Visual Studio, which project you want to execute when you press F5.

You can tell Visual Studio which project among the list of projects in the solution has to be started by right-clicking that project in the Solution Explorer and selecting the option “Set as Startup Project” from the context menu. Just by looking at the Solution Explorer you can say which one is the current startup project. The project name will appear in bold letters in the Solution Explorer window. If you look at the above image, you can easily say that WinFormAppDemo project is the one that will execute when you press F5. You can try it by pressing F5.

Remember, there are some projects that can be set as startup while others cannot. Console Applications, Windows Forms Application, Web Forms Application, WPF Application are some of the examples of project types that can be set as Startup Projects. Class Library, User Control Library are examples of project types that cannot be set as Startup Projects.

I will speak about these project types in detail in the coming chapters.

By now you should be familiar about Visual Studio, Project Templates, and how to create and execute them.

In the next Chapter, I will start with the C# Language.

Blogger Labels: Projects, Solutions, Windows Forms, Application, Language, .Net Framework, Visual Studio, Visual Basic

…. continued in next article “C# 4.0 – Ch:03:A – C# Language Basics

visit www.iGnani.com

MicroMind Information Systems
#4013, K.R. Road, Banashankari II Stage,
BANGALORE - 560070
KARNATAKA, INDIA
Phone: +91 80 26762747


No comments:

Post a Comment