Information Technology Reference
In-Depth Information
Suppose also that you are writing a program called
MyWidgets
, and you want to use the
SquareWidget
class. The code for the program is in a file called
MyWidgets.cs
and is shown in
the following example. The code simply creates an object of type
SquareWidget
and uses the
object's members.
using System;
class WidgetsProgram
{
static void Main( )
{
SquareWidget sq = new SquareWidget(); // From class library.
↑
Not declared in this assembly
sq.SideLength = 5.0; // Set the side length.
Console.WriteLine(sq.Area); // Print out the area.
}
↑
}
Not declared in this assembly
Notice that the code doesn't declare class
SquareWidget
. Instead, you use the class defined
in
SuperLib
. When you compile the
MyWidgets
program, however, the compiler must be aware
that your code uses assembly
SuperLib
so it can get the information about class
SquareWidget
.
To do this, you need to give the compiler a
reference
to the assembly, by giving its name and
location.
In Visual Studio, you can add references to a project in the following way:
Select the Solution Explorer and find the
References
folder underneath the project
name. The
References
folder contains a list of the assemblies used by the project.
Right-c ick the
References
folder and select Add Reference. There are five tab pages from
which to choose, allowing you to find the class library in different ways.
For our program, select the Browse tab and browse to the DLL file and select it.
Click the OK button, and the reference is added to the project.