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.

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

{

publicclassmaths

{

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

publicstaticlong add(long a, long b

{

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

}

publicstaticlong sub(long a, long b)

{

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

}

publicstaticlong 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:

Once you click on option then u will get a box like Add Referencesunder 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.

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

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

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

{

publicpartialclassForm1 : Form

{

public Form1()

{

InitializeComponent();

}

privatevoid Form1_Load(object sender, EventArgs e)

{

}

privatevoid 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);

}

}

privatevoid 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();

}

privatevoid btnClear_Click(object sender, EventArgs e)

{

txtInput1.Text = "";

txtInput2.Text = "";

txtInput3.Text = "";

}

privatevoid 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.