Using DLL files in Visual Studio 2005 using C#

Part 1: Creating a Class Library (DLL)

In this section, we will learn how to create a simple DLL file

1. Open a New Project as shows below and Select Class Library from the templates.

@1.gif

Select the File Name and the Location where you want to save this file.

2. This is what you will see on the main window.

@2.gif

Now create a simple function in the class Class1.

In this case I have created the highlighted function to add two numbers.

3. Now, select Build Solution from the Build Menu in the Menu Bar.

3.gif 

4. Here is the DLL file created in the Debug folder.

4.gif

Now this DLL file can be copied and pasted anywhere and can be used by any number of programs at a given point of time. 

Part 2: Using a Class Library (DLL)

In this section, we will learn how to use the DLL file that we have created.

NOTE:

Namespace we used : MyDLL

Class                            : Class1

Function                      :  addnumbers(int a. int b) 

1. Create a new Windows Application. 
5.gif

2. Build a Simple Windows Form as Shown Below.

first-6.gif
3. Now, we need to add the Reference of the DLL file that we had created earlier.

6.gif 

4. Select the DLL file from the browse tab.

7.gif 

5. Now, we will use the class and the namespace of the DLL file.

Click on the button to generate an EventHandler

8.gif

Make a Note of the namespace that has been added to the coding. 

6. Create a new object of the class. Now add the member function of the class with the help of the Object c.

9.gif

7. This is how the event handler will look like.

first10.gif

8. Now, run the program.
And, here is the output.

10.gif 

This is just a simple dll file created. We can create DLL file for any method and use it anywhere we like.


Similar Articles