How to Make DLL

Embed Size (px)

Citation preview

  • 8/6/2019 How to Make DLL

    1/17

    Creating a DLL using Visual C# is piece of cake. Believe me its much easier than VC++. I have divided this tutorial

    in two parts. 1. Building a Class Library, and 2. Building a client application to test the DLL.

    Part 1: Creating a Class Library (DLL)

    Create an Empty Class Library Project

    Select File->New->Project->Visual C# Projects->Class Library. Select your projectname and appropriate directory using Browse button and click OK. See Figure 1.

    Figure 1.

    Project and Its files

    The Solution Explorer adds two C# classes to your project. First is AssemblyInfo.cs andsecond is Class1.cs. We don't care about AssemblyInfo. We will be concentrating onClass1.cs. See Figure 2.

  • 8/6/2019 How to Make DLL

    2/17

    Figure 2.

    The mcMath Namespace

    When you double click on Class1.cs, you see a namespace mcMath. We will be referencingthis namespace in our clients to access this class library.

    using System;

    namespace mcMath{

    ////// Summary description for Class1.///publicclass Class1{

    public Class1()

    {//// TODO: Add constructor logic here//}}}

  • 8/6/2019 How to Make DLL

    3/17

    Now build this project to make sure every thing is going OK. After building this project, youwill see mcMath.dll in your project's bin/debug directory.

    Adding Methods

    Open ClassView from your View menu. Right now it displays only Class1 with no methods

    and properties. Lets add one method and one property. See Figure 3.

    Figure 3.

    Right click on Class1->Add->Add Method... See Figure 4.

  • 8/6/2019 How to Make DLL

    4/17

    Figure 4.

    C# Method Wizard pops up. Add your method name, access type, return type, parameters,and even comments. Use Add and Remove buttons to add and remove parameters fromthe parameter list respectively. I add one test method called mcTestMethod with noparameters. See Figure 5.

  • 8/6/2019 How to Make DLL

    5/17

    Figure 5.

    I am adding one more method long Add( long val1, long val2 ). This method adds two

    numbers and returns the sum. Click Finis

    hbutton w

    hen you're done. See Figure 6.

  • 8/6/2019 How to Make DLL

    6/17

    Figure 6.

    The above action adds two method to the class and methods look like following listing:

    ////// //This is a test method///publicvoid mcTestMethod(){

    }

    public long Add(long val1, long val2){

    }

    Adding Properties

    Open C# Property Wizard in same manner as you did in the case of method and add aproperty to your class. See Figure 7.

  • 8/6/2019 How to Make DLL

    7/17

    Figure 7.

    This action launches C# Property Wizard. Here you can type your property name, type and

    access. You alsohave options to c

    hoose from get only, set only or get and set bot

    h. You caneven select if a property is static or virtual. I add a property Extra with public access and

    bool type and get/set option set. See Figure 8.

  • 8/6/2019 How to Make DLL

    8/17

    Figure 8.

    After adding a method and a property, our class looks like Figure 9 in Class View after

    expanding the class node.

  • 8/6/2019 How to Make DLL

    9/17

    Figure 9.

    If you look your Class1 class carefully, Wizards have added two functions to your class.

    ////// //This is a test property///publicbool Extra{get{returntrue;}set{}}

    Adding Code to the Class

    Add this code ( bold ) to the methods and property now. And now I want to change myClass1 to mcMathComp because Class1 is quite confusing and it will create problem whenyou will use this class in a client application. Make sure you change class name and itsconstructor both.

    Note: I'm not adding any code to mcTestMethod, You can add any thing if you want.

  • 8/6/2019 How to Make DLL

    10/17

    using System;

    namespace mcMath{

    ///

    /// Summary description for Class1.///publicclass mcMathComp{privatebool bTest = false;

    public mcMathComp(){// TODO: Add constructor logic here}

    ////// //This is a test method///publicvoid mcTestMethod(){ }

    publiclong Add(long val1, long val2){return val1 + val2;}

    ////// //This is a test property///

    publicbool Extra{get{return bTest;}set{bTest = Extra ;}}}

    }

    Build the DLL

    Now build the DLL and see bin\debug directory of your project. You will see your DLL. Pieceof cake? Huh? :).

    Part 2: Building a Client Application

  • 8/6/2019 How to Make DLL

    11/17

    Calling methods and properties of a DLL from a C# client is also an easy task. Just followthese few simple steps and see how easy is to create and use a DLL in C#.

    Create a Console Application

    Select File->New->Project->Visual C# Projects->Console Application. I will test my

    DLL from this console application. See Figure 10.

    Figure 10.

    Add Reference of the Namespace

    Now next step is to add reference to the library. You can use Add Reference menu option toadd a reference. Go to Project->Add reference. See Figure 11.

  • 8/6/2019 How to Make DLL

    12/17

    Figure 11.

    Now on this page, click Browse button to browse your library. See Figure 12.

  • 8/6/2019 How to Make DLL

    13/17

    Figure 12.

    Browse for your DLL, which we created in part 1 of this tutorial and click Ok. See Figure 13.

  • 8/6/2019 How to Make DLL

    14/17

    Figure 13.

    Add Reference Wizard will add reference of your library to the current project. See Figure14.

  • 8/6/2019 How to Make DLL

    15/17

    Figure 14.

    After adding reference to mcMath library, you can see it as an available namespace

    references. See Figure 15.

  • 8/6/2019 How to Make DLL

    16/17

    Figure 15.

    Call mcMath Namespace, Create Object of mcMathComp and call its methods andproperties.

    You are only one step away to call methods and properties of your component. You followthese steps:

    1. Use namespace

    Addusing mcMath in the beginning for your project.

    using mcMath;

    2. Create an Object of mcMathComp

    mcMathComp cls = new mcMathComp();

    3. Call Methods and Properties

    Now you can call the mcMathComp class method and properties as you can see I call Addmethod and return result in lRes and print out result on the console.

    mcMathComp cls = new mcMathComp();long lRes = cls.Add( 23, 40 );cls.Extra = false;Console.WriteLine(lRes.ToString());

  • 8/6/2019 How to Make DLL

    17/17

    Now you can print out the result.

    The entire project is listed in the following Listing:

    using System;using mcMath;

    namespace mcClient{////// Summary description for Class1.///

    class Class1{

    //////The main entry point for the application.

    ///[STAThread]staticvoid Main(string[] args){mcMathComp cls = new mcMathComp();long lRes = cls.Add( 23, 40 );cls.Extra = false;Console.WriteLine(lRes.ToString());}}}

    Now build and run the project. The output looks like Figure 16.

    Figure 16.