Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,486 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,209 people online right now. Registration is fast and FREE... Join Now!




Creating a DLL to store common methods in C#

 
Reply to this topicStart new topic

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

aj32
Group Icon



post 10 Jun, 2008 - 07:45 PM
Post #1


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:
csharp

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! wink2.gif

Next, we would add our description to the method:
csharp

/// <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!)
csharp

/// <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:
csharp

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:
csharp

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#! smile.gif

I hope this tutorial was useful, and thanks for reading
- AJ32 smile.gif
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 11/22/08 03:39PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month