adding references

7
© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 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.

Upload: sazunax

Post on 07-May-2017

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Adding References

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

1Adding 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 yetread Chapter 3, please do so. Thank you.

Page 2: Adding References

60 Adding a Reference to a C# or Visual Basic .NET Project Appendix 1

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

Let us walk through an example in which Visual Studio indicates that a class cannotbe 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 ExperiencedProgrammers). This project uses an assembly reference to the TimeLibrary (or theEmployeeLibrary in the Visual Basic .NET books) project, shown in Figure 8.17 ofthe 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 thefollowing errors (the first image displays the errors for the C# application, while the secondimage 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 basedon 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 Explorerwith a small warning symbol, shown in the image below. It is always a good idea to viewthe References folder before compiling a project, to see if any references need to beupdated.

Fig. 1.1 Error when compiling project AssemblyTest.

Page 3: Adding References

Appendix 1 Adding a Reference to a C# or Visual Basic .NET Project 61

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

We now walk the reader through updating an assembly reference. To begin, removethe reference by right-clicking the reference in the Solution Explorer and selectingRemove (see image below).

Add a reference by right-clicking the References folder in the Solution Explorerand selecting Add Reference… (see image below). This will open the Add Referencedialog, 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.

Brokenreference

Page 4: Adding References

62 Adding a Reference to a C# or Visual Basic .NET Project Appendix 1

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

In the top-right of the dialog resides a Browse… button (first image below), enablingus to search for our DLL in a Select Component dialog (second image below). This fileis 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 arecopied by default into that project’s bin/Debug/ directory, and DLL’s referenced in aVisual 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 properDLL. The only downside of using this technique when programming is that now you maybe referencing an out-of-date DLL, because the original library may have been updated. Ifyou 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.

Page 5: Adding References

Appendix 1 Adding a Reference to a C# or Visual Basic .NET Project 63

© Copyright 1992–2002 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 theOpen 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

Page 6: Adding References

64 Adding a Reference to a C# or Visual Basic .NET Project Appendix 1

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

the Selected Components pane at the bottom of the Add Reference dialog (shownbelow). Finally, we add this file to our project by clicking the OK button.

Notice that we have now successfully added our reference (see Solution Explorerbelow). The program will now compile and run (see final figure, which displays first theC# 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.

Page 7: Adding References

Appendix 1 Adding a Reference to a C# or Visual Basic .NET Project 65

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

Fig. 1.9 Program executing successfully.