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

Creating and using dll libraries in C Sharp.Net


C# C# 
Ratings:
0 Comment(s)
 6886  View(s)
Rate this:

Creating and using dll libraries in C#.net

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.

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

Example

Add.cs

namespace MyMethods

{

//creating class AddClass

    public class AddClass

    {

//Creating Method

        public static long Add(long i, long j)

        {

            return (i + j);

        }

    }

}

 

Subtract.cs

namespace MyMethods

{

    public class SubtractClass

    {

        public static long 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’.

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.

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.

Coding

 

public partial class Form1 : 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();

        }

 

        private void 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();

        }

 

        private void 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();

        }

 

        private void 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


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

Follow MindStick