Home > DeveloperSection > Articles > HOW TO CREATE DLL AND USING THEM IN C#

HOW TO CREATE DLL AND USING THEM IN C#

Posted by  Amit Singh
on    September-15-2010 0:00 AM

C# C# 
Ratings:
4 Comment(s)
 26596  View(s)
Rate this:

HOW TO CREATE DLL AND USING THEM IN C#

We have to create DLL with the help of Class Library, and call in a program, here are the steps to create and how it works with the help of an example.

1st Step->open windows application we have to choose Class Library option on the right hand side under the template option and name them Createdll.

CREATE DLL

 

After doing this we have to write a piece of code in .cs file, for the application like Add, subtracts and multiplies. Don’t forget to include the namespace as we have mentioned as namespace CreateDll.  And create a class name as math, under that class you can place a piece of code for Function Add, similarly for Subtract and multiply, as shown below.

 

 

namespace Createdll

{

    public class maths

    {

     //using add function and taking two parameters a and b.

 

        public static long add(long a, long b     

       {

            return (a + b);//return sum of a and b.

        }

 

        public static long sub(long a, long b)

        {

            return (a - b);//return minus of a and b.

        }

        public static long mul(long a, long b)

        {

            return (a * b);//return multiply of a and b.

        }

    }

}

 

 

Next important part is how to build and add references  to the dll.For Building an DLL Just Press F5 or we can click on the file option under that we will find debug option once we click it will be build automatically.

Under the Solution explorer on the right hand side of the corner we can easily see an option like reference as shown below:

 

 

Adding reference of dll created

 

 

Once you click on option then u will get a box like Add References  under that you have to click Browse you need to ADD ur application where ur application Create DLL is placed then click bin then ur name of The application like as shown with the help of arrow.

Adding reference of dll created

After adding reference to createDll library, you can see it as an available namespace as shown below in the screenshot

Reference of DLL added

Next important part is we have to call that CREATE DLL in our application, for that we have to open new windows project  and create a form like that as shown as below .

using created dll

As soon as we have created the Form like above next we have to write a piece of code under the Add, subtract, multiply and clear button.

Don’t forget to place a name space CreateDll same as the DLL which we want to access here with the help of DLL, as follows.

 using Createdll;

place a piece of code under the various button click

namespace irfandll

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

        }

 

        private void btnAdd_Click(object sender, EventArgs e)

        {

            try

            {

                long a, b;

//assigning values of text boxes in variable a and b respectively.

                a = long.Parse(txtInput1.Text);

                b = long.Parse(txtInput2.Text);

//using add method of maths class I had created in class library (Createdll)

                txtInput3.Text = maths.add(a, b).ToString();

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

 

        private void btnSubtract_Click(object sender, EventArgs e)

        {

            long a, b;

//assigning values of text boxes in variable a and b respectively.

 

            a = long.Parse(txtInput1.Text);

            b = long.Parse(txtInput2.Text);

//using add method of maths class I had created in class library (Createdll)

 

            txtInput3.Text = maths.sub(a, b).ToString();

 

        }

 

 

 

        private void btnClear_Click(object sender, EventArgs e)

        {

            txtInput1.Text = "";

            txtInput2.Text = "";

            txtInput3.Text = "";

        }

        private void btnMultiply_Click(object sender, EventArgs e)

        {

            long a, b;

//assigning values of text boxes in variable a and b respectively.

 

            a = long.Parse(txtInput1.Text);

            b = long.Parse(txtInput2.Text);

//using add method of maths class I had created in class library (Createdll)

 

            txtInput3.Text = maths.mul(a, b).ToString();

        }

    }

}

 

 

After this process we have to build our application with the help of F5.we will see the output like as shown below

In these of the text field we need to check by putting various no in the text box and one by one we have to check the entire button as Add, Subtract, Multiply, and Clear button.

The desired output will be like that as shown below in the screen shot.


DLL

By John Smith on   3 years ago
Good Article Amit.

Nice Article

By aken H on   3 years ago

hi Amit Singh,

   this a nice article,if can you About dll files in the main purpose or role is what.

thanks!


Question

By kat harris on   2 years ago
This article was helpful but, not exactly what I was looking for. 
I'm defining a complicated parser and want to save it as a dll in VS 2010 C# how do I do that?


HOW TO CREATE DLL AND USING THEM IN C#

By Haider M Rizvi on   2 years ago

Hi kat harris,

Can you elaborate your problem.

Thanks.
















Recent Activities


Kamlakar Singh added new Blog Convert Text Document to PDF File   10 days ago

Kamlakar Singh added new Article Introduction to Backbone.js   10 days ago

Kamlakar Singh added new Question Explain the AdRotator Control.   11 days ago

Don't want to miss updates? Please click the below button!