Tuesday, June 7, 2011

How to create dll in c sharp

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();
            }
        }
    }
    

No comments:

Post a Comment

Your comment is pending for approval

AngularJS Basics - Part 1

                                                                  AngularJS What is AngularJS ·          Framework by googl...