Support us on YouTube by Subscribing our YouTube Channel. Click here to Subscribe our YouTube Channel

Friday, 7 February 2014

Creating and using DLL (Class library) in C#

A DLL (Dynamic Link library) is a library that contain some functions (code) and data which can be used by more than one program at a same time. Once we have created a DLL file, we can use it in many application. The only thing we have to do is to add the reference/Import the DLL File.


Creating DLL File:-
Step 1: Open Visual Studio -> Click on File->New Project -> Visual C# -> Class library

Step 2:- Change the class name(class1.cs) to calculate.cs

Step 3:- in calculate class write method for addition and subtraction of two integer.

Step 4:- Build the solution (F6). If Build is succeeded than you will see calculation.dll file in bin/debug directory of your project.
We have created our DLL file. Now we are going to use it in another Application.

Using DLL File:-
Step 1:- Open Visual Studio-> Click on File -> New Project ->Visual C# ->Windows Form Application
Step 2:- Design form as in below picture

Step 3:- Add Reference of dll file i.e. calculation.dll that we have created earlier.
Step: 4:- Add namespace (using calculation)




Step 5:-
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Calculation;

namespace MiniCalculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        calculate cal = new calculate();
        //Addition Button click event
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //storing the result in int i
                int i = cal.Add(int.Parse(txtFirstNo.Text), int.Parse(txtSecNo.Text));
                txtResult.Text = i.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //Subtraction button click event
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                //storing the result in int i
                int i = cal.Sub(int.Parse(txtFirstNo.Text), int.Parse(txtSecNo.Text));
                txtResult.Text = i.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

Protected by Copyscape Web Plagiarism Scanner







1 comment:

Subscribe us on YouTube

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Like us on Facebook