Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Create DLL with C# (C-sharp) (See related posts)


HowTo create a dynamically linked library (DLL) with C# (C-sharp) and
Visual Studio 2005.

HowTo create a dynamically linked library (DLL) with C# (C-sharp) and
Visual Studio 2005.

1. Creating the DLL:

a) Create a new project -> C# Classlibrary.

b) Create your class adding or modifying
a class-file to the project and entering
something like:
		using System;
		using System.Collections.Generic;
		using System.Text;

		namespace MyClassLib
		{
		    public class ClassA
		    {
			public int PropA = 10;

			public void Multiply()
			{
			    PropA *= 2;
			}

			public void Multiply(Int32 myFactor)
			{
			    PropA *= myFactor;
			}

		    }
	  


2. Test-bind the DLL

a) Create another project WITHOUT closing or deleting the old project.
(This is done by right-clicking on the Project-Explorer and
choosing Add->New Project)

b) The new project needs to now about the DLL. The terminology is
different from that of C++. Here, we talk about "references" instead
of links. Thus, we have to add the DLL to the reference tree of the
new project in the project-explorer. Rightclick on your
new project->add reference->choose tab:Project and add.

c) To make life easier, use the namespace of your dll. Your
main code could look something like this:

	   
	   using System;
	   using System.Collections.Generic;
	   using System.Text;
	   using MyClassLib;
	   
	   namespace ConsoleApplication1
	   {
	       class Program
	       {
	           static void Main(string[] args)
	           {
	               ClassA obj = new ClassA();
	               obj.PropA = 9;
	               obj.Multiply(9);
	               Console.WriteLine(obj.PropA);
	               Console.ReadLine();
	           }
	       }
	   }
	   


Remember: The force is with you!



You need to create an account or log in to post comments to this site.


Click here to browse all 5147 code snippets

Related Posts