Adding References
Adding References
Adding References
7/30/02
1
Adding a Reference
to a C# or Visual Basic
.NET Project
*This document assumes basic knowledge of C# or Visual Basic .NET. If you have not yet
read Chapter 3, please do so. Thank you.
60 Adding a Reference to a C# or Visual Basic .NET Project Appendix 1
Copyright 19922002 by Deitel & Associates, Inc. All Rights Reserved. 7/30/02
Let us walk through an example in which Visual Studio indicates that a class cannot
be found. For this example we use project AssemblyTest, shown in Fig. 8.19 in the book
(Fig. 5.19 in C# for Experienced Programmers and Visual Basic .NET for Experienced
Programmers). This project uses an assembly reference to the TimeLibrary (or the
EmployeeLibrary in the Visual Basic .NET books) project, shown in Figure 8.17 of
the book (again, 5.17 in C# for Experienced Programmers and Visual Basic .NET for Expe-
rienced Programmers). For some of you, opening and building this project will lead to the
following errors (the first image displays the errors for the C# application, while the second
image displays the errors for the Visual Basic .NET application):
These messages indicate that TimeLibrary.dll (or EmployeeLibrary.dll)
cannot be found. When this example was created, we added a reference to this DLL based
on our directory structure. Many of you will no doubt be using a different directory struc-
ture, and as a result the DLL will not be located in the same place. A missing reference
(sometimes known as a broken reference) will be displayed in the Solution Explorer
with a small warning symbol, shown in the image below. It is always a good idea to view
the References folder before compiling a project, to see if any references need to be
updated.
Fig. 1.1 Error when compiling project AssemblyTest.
Appendix 1 Adding a Reference to a C# or Visual Basic .NET Project 61
Copyright 19922002 by Deitel & Associates, Inc. All Rights Reserved. 7/30/02
We now walk the reader through updating an assembly reference. To begin, remove
the reference by right-clicking the reference in the Solution Explorer and selecting
Remove (see image below).
Add a reference by right-clicking the References folder in the Solution Explorer
and selecting Add Reference (see image below). This will open the Add Reference
dialog, which allows the programmer to search for an assembly.
Fig. 1.2 A broken reference in Visual Studio .NET.
Fig. 1.3 Removing a reference in Visual Studio .NET.
Broken
reference
62 Adding a Reference to a C# or Visual Basic .NET Project Appendix 1
Copyright 19922002 by Deitel & Associates, Inc. All Rights Reserved. 7/30/02
In the top-right of the dialog resides a Browse button (first image below), enabling
us to search for our DLL in a Select Component dialog (second image below). This file
is located in the TimeLibrary projects bin/Debug/ directory, or the EmployeeL-
ibrary projects bin directory. More conveniently, DLLs referenced in a C# project are
copied by default into that projects bin/Debug/ directory, and DLLs referenced in a
Visual Basic .NET project are copied by default into that projects bin directory. There-
fore, you can simply browse to this directory of project AssemblyTest to find the proper
DLL. The only downside of using this technique when programming is that now you may
be referencing an out-of-date DLL, because the original library may have been updated. If
you wish to use the updated DLL, browse to the bin/Debug/ directory of the TimeL-
ibrary project, or for VB .NET programmers, the bin directory of the EmployeeLi-
brary project. This file can be taken off of the CD if you cannot find it.
Fig. 1.4 Adding a reference in Visual Studio .NET.
Appendix 1 Adding a Reference to a C# or Visual Basic .NET Project 63
Copyright 19922002 by Deitel & Associates, Inc. All Rights Reserved. 7/30/02
Once the DLL has been found (either TimeLibrary.dll or EmployeeLi-
brary.dll, depending on which application you are running), select it and click the
Open button located on the lower right of the dialog (Fig. 1.6). This will add the DLL to
Fig. 1.5 Using the Add Reference dialog to add a reference in a C# project.
Fig. 1.6 Finding and selecting an assembly reference.
Browse
button
64 Adding a Reference to a C# or Visual Basic .NET Project Appendix 1
Copyright 19922002 by Deitel & Associates, Inc. All Rights Reserved. 7/30/02
the Selected Components pane at the bottom of the Add Reference dialog (shown
below). Finally, we add this file to our project by clicking the OK button.
Notice that we have now successfully added our reference (see Solution Explorer
below). The program will now compile and run (see final figure, which displays first the
C# program running successfully, then the VB .NET program running successfully).
Fig. 1.7 Adding a selected assembly to our project.
Fig. 1.8 TimeLibrary reference after it has been added.
Appendix 1 Adding a Reference to a C# or Visual Basic .NET Project 65
Copyright 19922002 by Deitel & Associates, Inc. All Rights Reserved. 7/30/02
Fig. 1.9 Program executing successfully.