|
Of solutions, projects and items
The C# Column - Yashawant Kanetkar
The
.NET architecture has provided a systematic organisation of files,
folders, data connections and references used in a program. The
organisation comes in the form of solutions and projects. A solution
is a container for one or more related projects, whereas, a project
is a container of various items comprising the application.
Multi-projects solution
As said above, a solution
can contain one or more projects. In addition to projects, a solution
can also contain solution items and miscellaneous items. We will
take a look at these items later in this article. A solution automatically
gets created when we create a project and carries the same name
as the project. However, we can choose to create separate folders
for solutions and projects by selecting the ‘Create directory for
Solution’ checkbox that appears on the ‘New Project’ dialog box.
But when are we required
to create multiple projects in one solution? There are many scenarios
when such a need arises. Suppose we have created an assembly exporting
a method that converts rupees to dollars and returns dollars as
an integer value. Then we create a console application as a client
of this assembly. We could close the project containing the assembly
and create the client application in the same instance of the IDE.
After the client is run
successfully, at one point we decide that the exported method should
return a float value so as to return the exact converted value.
So, we again close the client project and re-open the assembly project,
make changes in it and re-open the client project to test the assembly.
Someone would keep two instances of the Visual Studio.NET IDE open
and work in them simultaneously. Now, what if we want to create
two clients—one of them as a console application and another as
a form-based application. In such a case instead of keeping many
instances open, we can create all these projects in the same solution.
Creating multiple projects
in one solution enables us to manage and work on the projects in
one instance of the IDE. If we set the options or change the settings,
they apply to the group of projects. Again, we can use the Solution
Explorer window to manage and view all the projects and their items
at a glance.
The information about the
projects is stored in two files ‘.sln’ (solution definition file)
and ‘.suo’ (solution user options file). The solution definition
file stores the meta-data that defines the solution, projects that
are associated with the solution and items that you want to be available
to all the projects. It also contains any ‘Build’ configurations
created at the solution level. The ‘.suo’ file stores information
about the options that customise the IDE.
Let us see how to create
and manage multiple projects in a solution. Create a Class Library
project named ‘rupeetodollar’. Define a method in it as shown below:
public float todollar(int r)
{
return (r * 47.50f);
}
Create a console application
as the client of the ‘rupeetodollar.dll’ assembly. While creating
the project, select the ‘Add to Solution’ radio button on the ‘New
Project’ dialog box. After the project is created, Solution Explorer
displays the visual representation of solution as shown in the adjoining
figure.

The project ‘rupeetodollar’
appears in bold type. This is because it is, by default, set as
the start-up project. The start-up project is the one that gets
executed when Ctrl+F5 is pressed. We can change the Start-up project
by right-clicking the project name in Solution Explorer and selecting
the ‘Set as StartUp Project’ menu item. Although only the startup
project is executed, every time we build the solution (or press
Ctrl+F5), all its projects get built.
We can even select which
project to build and which not to build. For this, go to the Properties
window of the solution. Select ‘Configuration Properties’ item.
The right hand side pane displays the build configuration options.
Uncheck the ‘Build’ check box for the project you don’t want to
build and click the OK button. Now if you build the solution only
those projects that are selected will get built. In the ‘rupeetodollar’
example, once the assembly is created, we don’t want to build it
again (unless we change it). So, we can very well choose not to
build it.
We are going to create two
clients for the ‘rupeetodollar’ assembly. In this case, to save
the time, we can run both the client projects simultaneously by
setting the multiple startup projects. For this, go to the Properties
window of the solution. Select the ‘Multiple Startup Projects’ radio
button as shown in the following figure:

Only selecting multiple
startup projects option is not sufficient. We must also select some
action for the project. We have selected ‘Start without debugging’
from the ‘Action’ list for the client projects. Click the OK
button.
Add the references of the
‘rupeetodollar.dll’ assembly in both the projects and add the following
code snippet in the console application.
rupeetodollar.convert c = new rupeetodollar.convert(); float d = c.todollar (10); Console.WriteLine (d);
Add the following code snippet
in the form-based application.
rupeetodollar.convert c = new rupeetodollar.convert(); float d = c.todollar(10); MessageBox.Show (d.ToString());
Press Ctrl+F5. You will
see the output of both the projects simultaneously.
Adding too many projects
to a solution may make working with the solution slow. For example,
opening the solution in IDE takes more time. We can improve the
working time by temporarily excluding the project that we don’t
require often from the solution. To do so, select the project and
select the ‘Projects | Unload Project’ menu item. The unloaded project
doesn’t get built. We can reload it by right clicking it and selecting
‘Reload Project’ from the pop-up menu.
Solution items
Sometimes we need items
that are useful in developing the application but do not belong
to any particular project. These items are called as ‘Solution Items’
and are displayed under ‘Solution Items’ node in the Solution Explorer.
These files don’t get built.
A style sheet is a common
solution item. A style sheet can be used to format numerous Web
pages in multiple projects. When the solution is re-opened, the
style sheet file appears under the ‘Solution Items’ node in ‘Solution
Explorer’. Actually, the file representing a solution item can be
stored at any location on disk. The item displayed in the ‘Solution
Items’ acts as a link to this file. The ‘.sln’ file tracks the path
of the solution item file.
Miscellaneous files
Miscellaneous files are
the ones that we use for development of an application but are not
part of a project or solution. A common example of miscellaneous
items are the ‘Readme.txt’ files that are shipped with the software.
Similarly, development instructions, database schema, and code snippets
are examples of miscellaneous items.
To create a miscellaneous
item, select the ‘File | New | File’ option. Select a category and
then select the template. Click ‘Open’ button. Add the contents
to the file and save it. To display miscellaneous items in Solution
Explorer we need to select the ‘Tools’ menu and then select ‘Options
| Environment folder | Documents’. In the ‘Documents’ tab check
the ‘Show miscellaneous files in Solution Explorer’ checkbox.
Project as container
A project acts as a container
for source files, references, folders, etc, that are part of the
application. The build information, configuration settings and details
of project items are stored in the ‘.csproj’ file. If the project
is part of a multi-project solution, we can open the individual
project by opening the ‘.csproj’ file.
In every project two folders
namely ‘bin’ and ‘obj’ are created. The ‘bin’ folder contains a
sub-folder ‘Debug’. ‘Debug’ is the output directory of the application.
An output directory stores the output file of a project that is
typically ‘.exe’ or ‘.dll’. The ‘bin’ folder contains sub-folders
‘Debug’ or ‘Release’ depending upon the build configuration of the
project. The following figure shows how a solution is organised.

The figure clearly indicates
that Projects and Solution Items are part of a solution, Miscellaneous
Items are not.
 |
Yashavant Kanetkar, one of the first
Express Computer columnists, is an established software expert,
speaker and author with several best-sellers to his credit,
including titles like “Let Us C” and the “Fundas” series. Contact
him at kanet@nagpur.dot.net.in |
|