Including Class Definitions from Outside Sources


Before you can declare a class, the compiler must know the definition of the class. Earlier in this chapter you learned how to define your own classes with the class keyword. But suppose you want to use someone else's class, like one that Microsoft has defined. For example, let's suppose that you want to talk to a database like SQL Server. Microsoft provides several classes to do so, one of which is called System.Data.SqlClient .SqlConnection. The definition for this class is in a DLL file called System.Data.DLL, which is an assembly. Remember that in .NET terms assemblies are files that contain .NET executable code. To use the definitions in an external assembly, you have to tell Visual Studio to reference this external file. Without the reference it won't understand when you ask it to create an instance of the SqlConnection class.

To reference another assembly:

  1. In a Visual Studio Project, choose View > Solution Explorer.

  2. Right-click on the References item and choose Add Reference ( Figure 2.52 ).

    Figure 2.52. A number of elements in Solution Explorer have context menus . If you right-click on the references element you can add a reference to another assembly in your project.

    graphics/02fig52.gif

  3. You should see the Add Reference dialog. The dialog displays all the .NET assemblies stored in the installation directory \Windows\Microsoft.NET\Framework \ver and several other directories specified through registry keys under HKEY_ LOCAL_MACHINE\SOFTWARE \Microsoft.NET\Framework\Assembly Folders\ ( Figure 2.53 ).

    Figure 2.53. The Add Reference dialog lists all assemblies in \Windows\Microsoft.NET\Framework \ver and several other directories. Double-click on the entry you want to reference in your project.

    graphics/02fig53.gif

  4. Click on the assembly you wish to reference and then click the Select button or double-click on the entry.

    or

    If the assembly you're looking for isn't in the list click the Browse button. This will display the typical Select Component dialog ( Figure 2.54 ).

    Figure 2.54. If the assembly you're looking for isn't in the GAC you can browse for it manually.

    graphics/02fig54.gif

  5. You can select multiple entries from the list by repeating step 4. When you're done, click OK.

graphics/tick.gif Tips

  • Assembly is a term usually used to describe a DLL or an EXE that contains .NET-compatible code.

  • Unlike other languages, in C# you don't reference source code to add a definition for another class not in your project. Instead, you reference something already compiled (an assembly) in the form of a DLL or an EXE.

  • Web projects already have a number of assemblies referenced. If you look at your solution window (View > Solution Explorer) and expand the References branch (by clicking on the plus sign next to it) you will see several assemblies already referenced. Among them are assemblies for doing database work and for manipulating XML ( Figure 2.55 ).

    Figure 2.55. You can view a list of references in your project by opening the references branch in Solution Explorer.

    graphics/02fig55.gif

  • Finding the assembly and referencing it when you're writing your application is one thing, but finding the assembly when you run the application is something altogether different. When you add a reference to another assembly to your project, all that does is help you compile your code. Without the reference, the compiler wouldn't know if the code you typed was in fact accurate. However, when you run your project, in order for your Web application to find the assembly at runtime the assembly must be in one of two places. Here is the first one: Under the directory your application is running from, you could create a subdirectory called bin and copy the referenced DLLs there ( Figure 2.56 ). Visual Studio .NET automatically creates a bin subdirectory in your Web application directory when you first create the project. The second place is the Global Assembly Cache (GAC), a special directory for assemblies. It is one of the places the .NET Framework automatically looks for assemblies. The GAC is under your Windows\Assembly directory ( Figure 2.57 ).

    Figure 2.56. VS.NET automatically creates a bin subdirectory in your application directory. It takes all the code you enter in the project and builds a DLL from it and copies it to the bin directory. Your Web forms are left in the application directory.

    graphics/02fig56.jpg

    Figure 2.57. Navigating to Windows\Assembly shows a nice view of the GAC. If you were to navigate to the same directory through the command prompt, things would look a little different.

    graphics/02fig57.jpg

  • If you add a reference to an assembly not in the GAC, Visual Studio .NET copies the latest version of the assembly to the bin subdirectory of your Web application every time you compile your Web project.

  • You add an assembly to the GAC if you want to use the same assembly from multiple Web applications. If you didn't add the assembly to the GAC, each Web application would need its own copy.

  • Adding an assembly to the GAC requires that you digitally sign your DLL. This will be explained in Chapter 13, C# Web Projects. Digital signatures make it possible for the .NET runtime to detect if a hacker has tampered with the code.




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net