Creating a Library Using Visual Studio 2015

Introduction

Here we talk about Dynamic Link Libraries (DLLs). Some people also called them C# class libraries. DLLs play a very effective role in Windows programming. In a DLL file there is very much information present, like the library functions, methods and many other information that are used in a Windows program. When we make a simple program and build this program then automatically in your bin folder the DLL of that class file is created. There are two types of DLLs, one is Static DLL and another one is the Dynamic DLL. The Static DLL is in existence until and unless our program is in an active state but a Dynamic DLL is only in existence when it is required. Usually a Dynamic Link Library is used for the resources for the program. We can use a single DLL file in more than one program.

Now here we discuss how to create a DLL file in Visual Studio 2015 Preview. It is a very easy process. This article first shows how to create a DLL and then shows how to call the DLL file in our other program. So let's go with the given procedure.

Creating a DLL (Class Library file) in Visual Studio 2015 preview

Step 1

Open your Visual Studio 2015 preview, go to the file menu, go to the new, go with the project, from the template field select C#, select Class Library then name it as you want. Save it at an appropriate place. So from the browse button you can select the appropriate location for saving the DLL.



Step 2

After creating this Class Library (DLL) in the Solution Explorer you will see the class. In this class there is no method and no property at the initial level. This class is totally empty. We will now add to this class.



Step 3

As I said, there is one class that was generated named Class1. When we double-click on this class then we see the namespace rizDLL. We use this namespace for the further implementation, in other words when we create our Windows based program we use this namespace in our program. When you double-click on the class file you will see something like this.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace rizDLL  
  8. {  
  9.     /// <summary>   
  10.     /// Summary description for Class1.   
  11.     /// </summary>  
  12.     public class Class1  
  13.     {  
  14.         public Class1() // this is the constructor of class1  
  15.         {  
  16.             //   
  17.             // Add constructor logic here   
  18.             //   
  19.         }  
  20.     }  
  21. }  
Step 4

After doing this we will check whether everything in our project is okay. So for checking this we simply go to the tab menu then select build and click on it.



Step5

When you click on the build solution then in the output window you will see a message that the build is successful.



Now go to the location where you saved your rizDLL program. Open it, go to the bin folder, go to the debug folder in the debug folder you will see the DLL file named as you chose when creating the project. Here my DLL File name is rizDLL.dll.

Step 6

Now for adding the method and properties in the class file we simply go to the Solution Explorer then right-click on it and click on view Class Diagram.



When you click you have seen a Class diagram and in that diagram you will see an empty class.

Step 7

Now here we add a method to our class. For adding the method to our class we simply right-click on the class diagram and then we have an option, Add. Click on Add, now there are many options such as Add Method, Property, Field, Events and many more options.


 

When we click on the Add method then there is a method will Add to our class. Now in the bottom of our Visual Studio we have options to change the property of this method. Here we add two methods, one method name is mcTestMethod. In mcTestMethod we do not use any parameters. Another method name is Add Method, this method has three numbers and returns the sum of those numbers.



The overall process adds two methods to our class file and the methods look as in the given code.

  1. public void mcTestMethod()  
  2.       {  
  3.           throw new System.NotImplementedException();  
  4.       }  
  5.   
  6.       public long Add(long val1, long val2, long val3)  
  7.       {  
  8.           throw new System.NotImplementedException();  
  9.       }  
Step 8

Now we add a property to our class file. To add the property to our class file we simply follow the same procedure we followed to create the method. Simply go to the class diagram then right-click on it, click on Add, then click on the property. Here we add a property and name it Extra and typed it bool and used the public modifier with the get and the set properties.



Step 9

After Adding the property with the set and get method, add the following code to the class file. Here I change my class and constructor name, both for convenient implementation. Here we make the class name and constructor name usedll.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace rizDLL  
  8. {  
  9.     /// <summary>   
  10.     /// Summary description for Class1.   
  11.     /// </summary>  
  12.     public class usedll  
  13.     {  
  14.         private bool bTest = false;  
  15.         public usedll()  
  16.         {  
  17.             //  
  18.             // TODO: Add constructor logic here  
  19.             //  
  20.         }  
  21.   
  22.         /// <summary>  
  23.         /// //This is a test method  
  24.         /// </summary>  
  25.         public void mcTestMethod()  
  26.         {  
  27.   
  28.         }  
  29.         /// <summary>  
  30.         /// //This is a test property  
  31.         /// </summary>  
  32.         public bool Extra  
  33.         {  
  34.             get  
  35.             {  
  36.                 return bTest;  
  37.             }  
  38.             set  
  39.             {  
  40.                 bTest = Extra;  
  41.             }  
  42.         }  
  43.   
  44.         public long Add(long val1, long val2, long val3)  
  45.         {  
  46.             return val1 + val2 + val3;  
  47.         }  
  48.     }  
  49. }  
Step 10

After adding the code successfully we again build our solution and have a message in the output window that you have successfully built it. Now you can see your DLL code in your bin/debug folder.

Add DLL file in to the Another Program

Now I will explain how to use the rizDLL in your project. Use the following procedure.

Step 1

Again open Visual Studio 2015 Preview, go to the file menu, go to the project, add a new project then select the template for Visual C# then add a new console application.



Step 2

After that add the references for the library. So for this we go to the tab menu of Visual Studio then go to the tab project then go to the Add Reference command.



Step 3

When you click on Add References you will see a window like this.



Now simply click on the browse button and add the DLL file from the specific position where you saved it.



Step 4

After adding the DLL file in your console application, please open the Solution Explorer of the your console application. Explore the references in the references. You will see that DLL file.



Step 5

Now open the class file of the Console Application and add the namespace "using rizDLL".

Then add the following code to your console application file.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using rizDLL;  
  7.   
  8. namespace usingrizDll  
  9. {  
  10.     class Class1  
  11.     {  
  12.         /// <summary>  
  13.         /// The main entry point for the application.  
  14.         /// </summary>  
  15.         [STAThread]  
  16.         static void Main(string[] args)  
  17.         {  
  18.             usedll cls = new usedll();// object of dll class  
  19.             long lRes = cls.Add(23, 40,30);  
  20.             cls.Extra = false;  
  21.             Console.WriteLine("The sum of your three no is=" +lRes.ToString());  
  22.             Console.Read();  
  23.         }  
  24.     }  
  25. }  
Step 6

Now build your Console Application and Run it. After a successful build you will see the output as in the following picture.

Output



Summary

So this is the overall structure for making a DLL file and using the DLL file in another program in Visual Studio 2015 Preview. I think the procedure I told you is very easy for everyone to follow and make a DLL file and use the DLL file in another program. DLL files always play a very important role in Windows applications. Basically it works on the inheritance feature. I hope this article will be useful for the readers for creating a DLL and using the DLL in another program.


Similar Articles