Sie sind auf Seite 1von 7

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.

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/30/02
60 Adding a Reference to a C# or Visual Basic .NET Project Appendix 1

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):

Fig. 1.1 Error when compiling project AssemblyTest.

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.

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/30/02
Appendix 1 Adding a Reference to a C# or Visual Basic .NET Project 61

Broken
reference

Fig. 1.2 A broken reference in Visual Studio .NET.

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).

Fig. 1.3 Removing a reference in Visual Studio .NET.

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.

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/30/02
62 Adding a Reference to a C# or Visual Basic .NET Project Appendix 1

Fig. 1.4 Adding a reference in Visual Studio .NET.

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 project’s bin/Debug/ directory, or the EmployeeL-
ibrary project’s bin directory. More conveniently, DLL’s referenced in a C# project are
copied by default into that project’s bin/Debug/ directory, and DLL’s referenced in a
Visual Basic .NET project are copied by default into that project’s 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.

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/30/02
Appendix 1 Adding a Reference to a C# or Visual Basic .NET Project 63

Browse…
button

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.

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
© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/30/02
64 Adding a Reference to a C# or Visual Basic .NET Project Appendix 1

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.

Fig. 1.7 Adding a selected assembly to our project.

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.8 TimeLibrary reference after it has been added.

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/30/02
Appendix 1 Adding a Reference to a C# or Visual Basic .NET Project 65

Fig. 1.9 Program executing successfully.

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/30/02

Das könnte Ihnen auch gefallen