3D-kreativ
3D-kreativ

Reputation: 9319

Access class in dll?

From the Main class of the Windows Form I'm trying to create an object of a class inside another library(dll).

But I can't create this object, because I don't get any help fom Visual Studio, when I'm typing the name of that class.

I guess I need a reference to the library and I have tried to right click on the library and then Add Reference and selected the name of the project where I have my Main class and Windows Form.

But It still isn't working! Have I done this wrong or have I missed something?

Upvotes: 0

Views: 2286

Answers (2)

Adil
Adil

Reputation: 148180

Take the following steps to access the method in library (dll), MSDN

  • Right click the references folder in your form project add click add reference select the project or folder of dll.
  • Include the namespace of the dll in your class (Form) suppose you have namespace yourcomapany.communication then use using to include namespace to access classes in it.

    using yourcomapany.communication;

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1039448

But It still isn't working! Have I done wrong or I have I missed something?

Make sure that you have brought the namespace into which this class is defined into scope. So for example:

namespace FooBar
{
    public class Foo  
    {

    }
}

and then in your WinForms application after adding reference to the class library containing the class add the namespace:

using FooBar;

Now you could create instances:

Foo foo = new Foo();

Of course for this to work Foo must be declared as public. If it isn't then of course that you cannot access it. Remember that if there's no visibility modifier to the class:

namespace FooBar
{
    class Foo  
    {

    }
}

is equivalent to:

namespace FooBar
{
    internal class Foo  
    {

    }
}

so internal is assumed which means that you cannot access it from another assembly. It must be public.

Upvotes: 4

Related Questions