Class Library Projects


As well as placing classes in separate files within your project, you can also place them in completely separate projects. A project that contains nothing but classes (along with other relevant type definitions, but no entry point) is called a class library.

Class library projects compile into .dll assemblies, and you can gain access to their contents by adding references to them from other projects (which might be part of the same solution, but don't have to be). This extends the encapsulation that objects provide, because class libraries may be revised and updated without touching the projects that use them, allowing you to easily upgrade services provided by classes (which might affect multiple consumer applications).

The following Try It Out provides an example of a class library project and a separate project that makes use of the classes that it contains.

Try It Out – Using a Class Library

image from book
  1. Create a new project of type Class Library called Ch09ClassLib in the directory C:\BegVCSharp\Chapter9, as shown in Figure 9-10.

    image from book
    Figure 9-10

  2. Rename the file Class1.cs as MyExternalClass.cs (you can do this by right-clicking on the file in the Solution Explorer window and selecting Rename). Click Yes on the dialog that appears.

  3. Note that the code in MyExternalClass.cs automatically changes to reflect this class name change:

     public class MyExternalClass { } 
  4. Add a new class to the project, using the filename MyInternalClass.cs.

  5. Modify the code to make the class MyInternalClass internal:

     internal class MyInternalClass { }
  6. Compile the project (note that this project has no entry point, so you can't run it as normal — instead you can build it by selecting the Build Build Solution menu option).

  7. Create a new console application project called Ch09Ex02 in the directory C:\BegVCSharp\ Chapter9.

  8. Select the Project Add Reference... menu item, or select the same option after right-clicking on References in the Solution Explorer window.

  9. Click on the Browse tab, navigate to C:\BegVCSharp\Chapter9\Ch09ClassLib\bin\ Debug\, and double-click on Ch09ClassLib.dll.

  10. When the operation completes, check that a reference has been added in the Solution Explorer window, as shown in Figure 9-11.

    image from book
    Figure 9-11

  11. Open the Object Browser window and examine the new reference to see what objects it contains, as shown in Figure 9-12.

    image from book
    Figure 9-12

  12. Modify the code in Program.cs as follows:

    using System; using System.Collections.Generic; using System.Text; using Ch09ClassLib;     namespace Ch09Ex02 {    class Program    {       static void Main(string[] args) { MyExternalClass myObj = new MyExternalClass(); Console.WriteLine(myObj.ToString()); Console.ReadKey();       }    } }
  13. Run the application. The result is shown in Figure 9-13.

    image from book
    Figure 9-13

How It Works

In this example you have created two projects, one class library project and one console application project. The class library project, Ch09ClassLib, contains two classes: MyExternalClass — which is publicly accessible — and MyInternalClass — which is internally accessible. The console application, Ch09Ex02, contains simple code that makes use of the class library project.

To use the classes in Ch09ClassLib, you added a reference to Ch09ClassLib.dll to the console application. For the purposes of this example, you simply pointed at the output file for the class library, although it would have been just as easy to copy this file to a location local to Ch09Ex02, allowing you to continue development of the class library without affecting the console application. To replace the old version of the assembly with the new one, you simply copy the newly generated DLL file over the old one.

After adding the reference you took a look at the available classes using the object browser. Since one of the two classes (MyInternalClass) is internal, you can't see it in this display — it isn't accessible to external projects. However, the other class (MyExternalClass) is accessible, and this is the one you use in the console application.

You could replace the code in the console application with code attempting to use the internal class as follows:

static void Main(string[] args) { MyInternalClass myObj = new MyInternalClass();    Console.WriteLine(myObj.ToString());    Console.ReadKey(); }

If you attempt to compile this code, you will receive the following compilation error:

'Ch09ClassLib.MyInternalClass' is inaccessible due to its protection level

This technique of making use of classes in external assemblies is key to programming with C# and the .NET Framework. It is in fact exactly what you are doing when you make use of any of the classes in the .NET Framework, because they are treated in the same way.

image from book




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

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