Page 1 of 1

Creating a DLL to store common methods in C# A Beginner's Tutorial on Creating a DLL

#1 aj32  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 30
  • View blog
  • Posts: 577
  • Joined: 30-August 07

Post icon  Posted 10 June 2008 - 08:45 PM

*
POPULAR

Creating a DLL to store common methods in C#

A Beginner's Tutorial on Creating a DLL


DLL stands for "Dynamic-link Library", in this tutorial, I am going to show you how to create a DLL to store methods that you may use many times in different applications.

First, open visual studio (or any other .NET IDE), and create a new project, you will want to select "Class Library" as your project type, once you have created your project, the code shown should look something like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyFirstDLL
{
public class Class1
{
}
}


(I used the name 'myfirstDll' for the project name.)

It is actually rather simple to add methods to this, let's say I want to include a function that shows how many characters are in a string (this snippet), first we would create a public static string GetCharacters(string input), this would be the method used. Note that the method declaration is like a normal one, but with the added static parameter.
Note: you must declare the method as 'public' or else it will not work right! ;)

Next, we would add our description to the method:
/// <summary>
/// Counts the number of times a character appears in a string.
/// </summary>
/// <param name="input">The string to parse</param>
/// <returns></returns>
public static string GetCharacters(string input)
{
}



Now that we have that set up, we will add the code (this is just from the snippet, but modified to work with a DLL!)
/// <summary>
/// Counts the number of times a character appears in a string.
/// </summary>
/// <param name="input">The string to parse</param>
/// <returns></returns>
public static string GetCharacters(string input)
{
string[,] alphanumerics = { { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d", "e", "f", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" } };
string result = "";
int FI = 0;
int LI = 0;
int counts = 0;

for (int i = 0; i < 35; i++)
{
//Get the lower-case characters first//
FI = input.IndexOf(alphanumerics[0, i]);
while (FI > -1)
{
counts++;
LI = FI + 1;
FI = input.IndexOf(alphanumerics[0, i], LI);
}
if (counts > 0)
{
result += "There are " + counts + " " + alphanumerics[0, i] + "'s.\n";
}


//Count upper-case characters//
FI = input.IndexOf(alphanumerics[0, i].ToUpper());
while (FI > -1)
{
counts++;
LI = FI + 1;
FI = input.IndexOf(alphanumerics[0, i].ToUpper(), LI);
}
if (counts > 0)
{
result += "There are " + counts + " " + alphanumerics[0, i].ToUpper() + "'s.\n";
}
counts = 0;
}

return result;
}



Now, we are done with the DLL! With it all completed, it should look like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyFirstDLL
{
    public class Class1
    {


        /// <summary>
        /// Counts the number of times a character appears in a string.
        /// </summary>
        /// <param name="input">The string to parse</param>
        /// <returns></returns>
        public static string GetCharacters(string input)
        {

            string[,] alphanumerics = { { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d", "e", "f", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" } };

            string result = "";

            int FI = 0;
            int LI = 0;
            int counts = 0;


            for (int i = 0; i < 35; i++)
            {

                //Get the lower-case characters first//

                FI = input.IndexOf(alphanumerics[0, i]);
                while (FI > -1)
                {
                    counts++;
                    LI = FI + 1;

                    FI = input.IndexOf(alphanumerics[0, i], LI);
                }

                if (counts > 0)
                {
                    result += "There are " + counts + " " + alphanumerics[0, i] + "'s.\n";
                }



                //Count upper-case characters//

                FI = input.IndexOf(alphanumerics[0, i].ToUpper());
                while (FI > -1)
                {
                    counts++;
                    LI = FI + 1;

                    FI = input.IndexOf(alphanumerics[0, i].ToUpper(), LI);
                }

                if (counts > 0)
                {
                    result += "There are " + counts + " " + alphanumerics[0, i].ToUpper() + "'s.\n";
                }

                counts = 0;
            }


            return result;

        }


    }
}



Compile it and start another project, but this time a windows forms app or a console application.


Calling the methods in the DLL from another program

When we need to call that function, we will just need to add a reference to that DLL in our project (the project that will need to access the methods in the DLL).
To do this, (in VS), click Project->Add References on the menu bar, when the 'add references' dialog appears, click 'browse', select the compiled DLL (Usually in the 'bin/release' folder under the DLL Project files) and click OK.

Once this is done, you can call the method like this:
Console.WriteLine(MyFirstDLL.Class1.GetCharacters("The quick brown fox jumped over the lazy Dog!"));
// OR:
MessageBox.Show(MyFirstDLL.Class1.GetCharacters("Using DLLs is easy!"));


Assuming you have correctly built the DLL, it should write out the count of each characters in the string!

And that's it! Using DLL to store methods in this manner is actually quite easy with C#! :)

I hope this tutorial was useful, and thanks for reading
- AJ32 :)

Is This A Good Question/Topic? 7
  • +

Replies To: Creating a DLL to store common methods in C#

#2 shadowstep0705  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 48
  • Joined: 26-May 10

Posted 27 May 2010 - 09:23 AM

nice tutorial, i didn't know this before, but a question: if you update you're .dll file, do you have to remove it from the references and then add it back, or does he do this automatically?
Was This Post Helpful? 0
  • +
  • -

#3 javafreak  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 21-September 09

Posted 21 August 2011 - 06:46 PM

i think it will done automatically
Was This Post Helpful? 0
  • +
  • -

#4 cfsharp  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 10
  • Joined: 29-May 11

Posted 27 August 2011 - 09:13 AM

View Postshadowstep0705, on 27 May 2010 - 09:23 AM, said:

nice tutorial, i didn't know this before, but a question: if you update you're .dll file, do you have to remove it from the references and then add it back, or does he do this automatically?


No you wouldn't, but when you add the reference you have to put it in your using statements, as well as right clicking on your project in the file tree and clicking add reference.

And when you update the dll make sure you put it in the same folder as your program.
Was This Post Helpful? 0
  • +
  • -

#5 javafreak  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 21-September 09

Posted 07 September 2011 - 01:21 AM

how to merge it with exe i mean dll+exe=exe
Was This Post Helpful? 0
  • +
  • -

#6 NurulH  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 16
  • Joined: 18-January 11

Posted 08 September 2011 - 06:07 PM

View Postjavafreak, on 07 September 2011 - 01:21 AM, said:

how to merge it with exe i mean dll+exe=exe


Try ILMerge.
Was This Post Helpful? 1
  • +
  • -

#7 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Posted 26 December 2012 - 12:11 AM

Thanks for the tutorial! Exactly what I needed right now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1