Creating a DLL using Visual C# ispiece of cake. Believe me its much easier than VC++. I have divided thistutorial in two parts. 1. Building a Class Library, and 2. Building aclient application to test the DLL.
Part 1: Creating a Class Library (DLL)
Createan Empty Class Library Project
Select File->New->Project->VisualC# Projects->Class Library. Select your project name andappropriate directory using Browse button and click OK. See Figure 1.
Figure 1.
Projectand Its files
The Solution Exploreradds two C# classes to your project. First is AssemblyInfo.csand second is Class1.cs. We don't care about AssemblyInfo. Wewill be concentrating on Class1.cs. See Figure 2.
Figure 2.
ThemcMath Namespace
When you double click onClass1.cs, you see a namespace mcMath. We will be referencing thisnamespace in our clients to access this class library.
using System;
namespace mcMath
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
public Class1()
{
//
// TODO: Add constructor logic here
//
}
}
}
Now build this projectto make sure every thing is going OK. After building this project, youwill see mcMath.dll in your project's bin/debug directory.
Adding Methods
OpenClassView from your View menu. Right now it displays only Class1 with nomethods and properties. Lets add one method and one property. SeeFigure 3.
Figure 3.
Right click on Class1->Add->AddMethod... See Figure 4.
Figure 4.
C# Method Wizard popsup. Add your method name, access type, return type, parameters, and evencomments. Use Add and Remove buttons to add and remove parameters fromthe parameter list respectively. I add one test method calledmcTestMethod with no parameters. See Figure 5.
Figure 5.
I am adding one moremethod long Add( long val1, long val2 ). This method adds twonumbers and returns the sum. Click Finish button when you're done. SeeFigure 6.
Figure 6.
The above action addstwo method to the class and methods look like following listing:
AddingProperties
Open C#Property Wizard in same manner as you did in the case of method and add aproperty to your class. See Figure 7.
Figure 7.
This action launches C#Property Wizard. Here you can type your property name, type and access.You also have options to choose from get only, set only or get and setboth. You can even select if a property is static or virtual. I add aproperty Extra with public access and bool type and get/set option set.See Figure 8.
Figure 8.
After adding a methodand a property, our class looks like Figure 9 in Class View afterexpanding the class node.
Figure 9.
If you look your Class1class carefully, Wizards have added two functions to your class.
/// <summary>
/// //This is a test property
/// </summary>
public bool Extra
{
get
{
returntrue;
}
set
{
}
}
Adding Code to the Class
Add thiscode ( bold ) to the methods and property now. And now I want tochange my Class1 to mcMathComp because Class1 is quiteconfusing and it will create problem when you will use this class in aclient application. Make sure you change class name and its constructorboth.
Note:I'm not adding any code to mcTestMethod, You can add any thing if youwant.
using System;
namespace mcMath
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class mcMathComp
{
private bool bTest = false;
public mcMathComp()
{
// TODO: Add constructor logic here
}
/// <summary>
/// //This is a test method
/// </summary>
public voidmcTestMethod()
{ }
public long Add(long val1, long val2)
{
return val1 +val2;
}
/// <summary>
/// //This is a test property
/// </summary>
public bool Extra
{
get
{
return bTest;
}
set
{
bTest = Extra ;
}
}
}
}
Build the DLL
Now build the DLL andsee bin\debug directory of your project. You will see your DLL. Piece ofcake? Huh? :).
Part2: Building a Client Application
Callingmethods 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 usea DLL in C#.
Create a Console Application
Select File->New->Project->VisualC# Projects->Console Application. I will test my DLL from thisconsole application. See Figure 10.
Figure 10.
AddReference of the Namespace
Now next step is to addreference to the library. You can use Add Reference menu option to add areference. Go to Project->Add reference. See Figure 11.
Figure 11.
Now on this page, clickBrowse button to browse your library. See Figure 12.
Figure 12.
Browse for your DLL,which we created in part 1 of this tutorial and click Ok. See Figure 13.
Figure 13.
Add Reference Wizardwill add reference of your library to the current project. See Figure14.
Figure 14.
After adding referenceto mcMath library, you can see it as an available namespace references.See Figure 15.
Figure 15.
CallmcMath Namespace, Create Object of mcMathComp and call its methods andproperties.
You are only one stepaway to call methods and properties of your component. You follow thesesteps:
1. Use namespace
Add usingmcMath in the beginning for your project.
2. Create an Object ofmcMathComp
mcMathComp cls = new mcMathComp();
3. Call Methods andProperties
Now you can call themcMathComp class method and properties as you can see I call Add methodand return result in lRes and print out result on the console.
Now you can print out the result.
The entire project islisted in the following Listing:
Now build and runthe project. The output looks like Figure 16.
Figure 16.
联系客服