articles

Home / DeveloperSection / Articles / Creating and using dll libraries in C Sharp.Net

Creating and using dll libraries in C Sharp.Net

Uttam Misra10362 30-Jul-2010

Creating Class library

To create class library in C#, goto New Project in File Menu, a dialog box will appear, select Class Library icon in Visual C#->Windows option, and change its name to your desired name, here I have used MyMethod.

Creating and using dll libraries in C Sharp.Net










Now create class and add methods. Here in the example, I have created two classes.

Example

Add.cs

namespace MyMethods
{
//creating class AddClass
    publicclassAddClass
    {
//Creating Method
        publicstaticlong Add(long i, long j)
        {
            return (i + j);
        }
    }
}

 

Subtract.cs

namespace MyMethods
{
    publicclassSubtractClass
    {
        publicstaticlong Subtract(long i, long j)
        {
            return (i - j);
        }
    }
}

Note: we can create more than one method in a class ans more than one class in a namespace.

Now, once classes are created we need to build it so as to create its dll. To build the class library press ‘F6’ from key board or in ‘Build’ menu select ‘Build Solution’

To see the build dll check it in bin\debug folder of project.

Using class library in project

To use class library first of all we need to add it refrence in our project. To add its refrence right click on ‘Solution Explorer’ and select ‘Add Refrence’.

Creating and using dll libraries in C Sharp.Net








A dialog box will apear. In that dialog box click ‘Browse’ and select the dll present in your bin\debug folder of class library project.

Creating and using dll libraries in C Sharp.Net










Now add a namespace to your project. Here I have added

using MyMethods;

As MyMethod is the Class Library I have created.

To demonstrate the usage of class library I have designed a claculator which has the capability to add or subtract two numbers.

Creating and using dll libraries in C Sharp.Net



Coding
  
publicpartialclassForm1 : Form
    {
        long a, b;
        bool choice; //to check for which button press, add or subtract, true //for add and false for subtract.
 
      public Form1()
        {
            InitializeComponent();
        }
 
        privatevoid btnAdd_Click(object sender, EventArgs e)
        {    
//checking whether text box contains any value or not.      
            if (txtVal.Text != "")
            {
//converting value of text box to long
                a = long.Parse(txtVal.Text);
                txtVal.Text = "";
            }
            else
                a = 0;
//setting choice to true as add button is clicked
            choice = true;
            txtVal.Focus();
        }
 
        privatevoid btnSub_Click(object sender, EventArgs e)
        {
            if (txtVal.Text != "")
            {
                a = long.Parse(txtVal.Text);
                txtVal.Text = "";
            }
            else
                a = 0;
//seeting value of choice to true as subtract button is clicked
            choice = false;
            txtVal.Focus();
        }
 
        privatevoid btnResult_Click(object sender, EventArgs e)
        {
//assigning second value to text box to b
            if (txtVal.Text != "")
                b = long.Parse(txtVal.Text);
            else
                b = 0;
//checking whether choice is true or false.
            if (choice == true)
//calling AddClass, Add method created in MyMtehod Library
                txtVal.Text = AddClass.Add(a, b).ToString();
            else
//calling SubtractClass, Subtract method created in MyMtehod Library
                txtVal.Text = SubtractClass.Subtract(a, b).ToString();
            txtVal.Focus();
        }                  
    }
Screen shot

Creating and using dll libraries in C Sharp.Net








Updated 04-Mar-2020
More than 18 years of working experience in IT sector. We are here to serve you best.

Leave Comment

Comments

Liked By