DZone 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

Snippets has posted 5883 posts at DZone. View Full User Profile

Create DLL With C# (C-sharp)

04.23.2007
| 110579 views |
  • submit to reddit
        
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!


    

Comments

Snippets Manager replied on Fri, 2009/08/21 - 8:11am

This is only for console application what about win forms application? I want to call C# Windows application including Win forms form Vb.net application. Actually I am working in a team where half member are working on csharp and core members are working on VB.NET, so I want to call csharps forms from VB.NET main form. Please help me in this reagard