Home

Introduction to Custom Libraries in C#

   

Introduction to Custom Libraries

 

Introduction

The .NET Framework is a huge library made of various classes aimed at different scenarios. We will explore some of these classes in our lessons. Still, if the .NET Framework does not provide a functionality you are looking for, you can create you own library and use it in one or more programs.

You can even create a commercial library and distribute or sell it. For example, you can "package" one or more classes in a library.

A library is a program that contains classes and/or other resources that other programs can use. Such a program is created with the same approach as the programs we have done so far. Because a library is not an executable, it does not need the Main() function. A library usually has the extension .dll (other types of libraries in Microsoft Windows can have the extension .lib or another extension).

 

ApplicationApplication: Introducing Custom Libraries

  1. Start Microsoft Visual C#
  2. To start a new application, on the main menu, click File -> New Project...
  3. In the left list, click Windows
  4. In the right list, click Empty Project
  5. Change the Name to SaleRecord and click OK
  6. To save the project, on the Standard toolbar, click the Save All button Save All
  7. If you accept the suggest folder (in the Location), make a note of it. Otherwise, enter a different location and keep it in mind
  8. Click Save

Creating a Library

A library can be made of a single file or as many files as necessary. A file that is part of a library can contain one or more classes. Each class should implement a behavior that can eventually be useful and accessible to other classes. The classes in a library are created exactly like those of other programs.

To create a library, on the main menu of Microsoft Visual Studio, you can click File -> New Project... In the middle list, you can click Empty Project or click Class Library. Then give it a name:

New Project

If you had selected Empty Project in the New Project dialog box, you should open the Properties window and, in the Output Type combo box, select Class Library:

Properties

In both cases, a skeleton code would be presented to you and you can complete it as you see fit. Here is an example:

public class Algebra
{
        public double Addition(double x = 0, double y = 0)
        {
            return x + y;
        }

        public double Subtraction(double x = 0, double y = 0)
        {
            return x - y;
        }

        public double Multiplication(double x = 0, double y = 0)
        {
            return x * y;
        }

        public double Division(double x = 0, double y = 1)
        {
            return x / y;
        }
}

ApplicationApplication: Starting a Library

  1. On the main menu, click Project -> SaleRecord Properties...
  2. In the Output Type combo box, select Class Library
     
    Properties
  3. To create a new file, on the main menu, click Project -> Add New Item...
  4. In the left list, click Code
  5. In the right list, click Code File
  6. Change the Name to StoreItem and click Add
  7. Change the document as follows:
    public class StoreItem
    {
        public int     itemNumber;
        public string  itemName;
        public string  size;
        public decimal unitPrice;
    }
  8. Save all

Building a Library

Since you would be creating a library and not an executable, to compile the project:

  • On the main menu, you can click Build -> Build ProjectName
  • In the Solution Explorer, you can right-click the name of the project and click Build
  • In the Class View, you can right-click the name of the project and click Build

If you want to compile a library at the Command Prompt, you would type:

csc /target:library NameOfFile.cs

and press Enter.

ApplicationApplication: Building a Library

  • To create the library, on the main menu, click Build -> Build Solution

Using a Custom Library

After building the project, you can use it. You can use it in the same project where the library was built or you can use it in another project. If you are working in Microsoft Visual Studio, you can start by creating a new project. To use the library, you would have to reference the library. To do this:

  • On the main menu, you would click Project -> Add Reference...
  • In the Solution Explorer, you would right-click References and click Add Reference...
     
    Add Reference

In both cases, the Add Reference dialog box would come up. You can click the Browse tab, locate the folder where the library resides and select it:

Add Reference

After selecting the library, you can click OK. You can then use the classes and methods of the library like you would use those of the .NET Framework. Here is an example:

using System;

public class Exercise
{
    static void Main()
    {
	Algebra alg = new Algebra();

        double number1 = 244.58;
        double number2 = 5082.88;
        double result = alg.Addition(number1, number2);

        Console.Write(number1);
        Console.Write(" + ");
        Console.Write(number2);
        Console.Write(" = ");
        Console.WriteLine(result);
    }
}

If you want to compile the project at the Command Prompt, you would type something like the following:

csc /target:library /out:DesiredNameOfLibrary.dll NameOfFile.cs

This would produce:

244.58 + 5082.88 = 5327.46

Press any key to continue . . .

ApplicationApplication: Using a Custom Library

  1. To start a new project, on the main menu, click File -> New Project...
  2. In the middle list, click Empty Project
  3. Change the Name to DepartmentStore5 and click Add
  4. In the Solution Explorer, under DepartmentStore5, right-click References and click Add Reference...
  5. Click the Browse tab
  6. Locate the folder where the library was created
  7. Select SaleRecord.dll
  8. Click OK
  9. In the Solution Explorer, right-click DepartmentStore5 -> Add -> New Item...
  10. In the middle list, click Code File
  11. Change the name to DepartmentStore and click Add
  12. Change the file as follows:
    using System;
    
    public class Program
    {
        static int Main()
        {
            StoreItem si = new StoreItem();
    
            si.itemNumber = 660284;
            si.itemName = "Tropical Wool Neutral Jacket";
            si.unitPrice = 200.00M;
    
            Console.WriteLine("Store Inventory");
            Console.Write("Item #:     ");
            Console.WriteLine(si.itemNumber);
            Console.Write("Item Name:  ");
            Console.WriteLine(si.itemName);
            Console.Write("Unit Price: ");
            Console.WriteLine(si.unitPrice);
            Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
    
            System.Console.ReadKey();
            return 0;
        }
    }
  13. Execute the application:
    Store Inventory
    Item #:     660284
    Item Name:  Tropical Wool Neutral Jacket
    Unit Price: 200.00
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    Press any key to continue . . .
  14. Press any key and return to your programming environment
 

Home Copyright © 2010-2011 FunctionX