Monday, 4 February 2013

Creating C# Class Library (DLL) Using Visual Studio .NET



Part 1: Creating a Class Library (DLL)  

Create an Empty Class Library Project Select File->New->Project->Visual C# Projects->Class Library. Select your project name and appropriate directory using Browse button and click OK. 
See Figure 1.

Figure 1.


Project and Its files  

The Solution Explorer adds two C# classes to your project. First is AssemblyInfo.cs and second is Class1.cs.  We don't care about AssemblyInfo. We will be concentrating on Class1.cs.  
See Figure 2.

Figure 2

The NewDLL Namespace 

When you double click on Class1.cs, you see a namespace NewDLL. We will be referencing this namespace in our clients to access this class library. 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NEWDLL
{
    public class Class1
    {
       
    }
}


Adding Methods 

namespace NEWDLL
{
    public class Class1
    {
        public static int add(int value1, int value2)
        {
            int c = value1 + value2;
            return c;
        }
        public static int sub(int value1, int value2)
        {
            int c = value1 - value2;
            return c;
        }
    }
}


Build the DLL


Figure 3



Part 2: Building a Client Application

Calling methods and properties of a DLL from a C# client is also an easy task. Just follow these few simple steps and see how easy is to create and use a DLL in C#.

Create a Console Application   Select File->New->Project->Visual C# Projects-> Windows Forms Application. I will test my DLL from this console application. See Figure 4.


Add Reference of the Namespace



Now next step is to add reference to the library. You can use Add Reference menu option to add a reference. Go to Project->Add reference. See Figure 5

Figure 5.


Now on this page, click Browse button to browse your library. See Figure 6.

Figure 6


After adding reference to NewDLL library, you can see it as an available namespace references. See Figure 7

Figure 7



You are only one step away to call methods and properties of your component. You follow these steps:

1. Use namespace

Add using NEWDLL in the beginning for your project.
 

    using NEWDLL;


Add Code
private void button1_Click(object sender, EventArgs e)
{
     int value1=Convert.ToInt32(textBox1.Text);
     int value2=Convert.ToInt32(textBox2.Text);
     int result = NEWDLL.Class1.add(value1, value2);
     textBox3.Text = result.ToString();
}

private void button2_Click(object sender, EventArgs e)
{
            int value1 = Convert.ToInt32(textBox1.Text);
            int value2 = Convert.ToInt32(textBox2.Text);
            int result = NEWDLL.Class1.sub(value1, value2);
            textBox3.Text = result.ToString();
}



Form Design / Full Code: 


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 NEWDLL;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int value1=Convert.ToInt32(textBox1.Text);
            int value2=Convert.ToInt32(textBox2.Text);
            int result = NEWDLL.Class1.add(value1, value2);
            textBox3.Text = result.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int value1 = Convert.ToInt32(textBox1.Text);
            int value2 = Convert.ToInt32(textBox2.Text);
            int result = NEWDLL.Class1.sub(value1, value2);
            textBox3.Text = result.ToString();
        }
    }

No comments:

Post a Comment