Ori
Ori

Reputation: 1740

How can I use objects/classes from another namespace in C# (visual studio 2013)

I have a few differenet namespaces in my solution and I want to use an object called "Doctor" from namespece BL_Backend inside another namespace called DAL. I've tried adding a Refference to DAL (a refference for BL_Backend), and then adding "using BL_Backend;" but it wouldn't work. still classes such as Doctor won't appear as known ones inside namespace DAL.

namespace BL_Backend
{
…
namespace DAL
{
    …
    //Here create object as "Doctor" for BL_Backend class
}
}

For example, when i call a constructor of a doctor from DAL it says this constructor does not exists but when i write the exact same command at namespace bl_backend it works fine.

thanks alot!

Upvotes: 0

Views: 2168

Answers (3)

norbertVC
norbertVC

Reputation: 475

Classes without a modifier are internal by default, so ensure that your modifier is public. As a second option pay attention to the build order - its not a problem in VS12+ anymore, but in further versions it was.

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236328

I assume Doctor is declared as internal class in BL_Backend assembly. Note - if class does not have public access modifier, then by default it will be internal:

namespace BL_Backend
{
    class Doctor // this class is internal
    {

    }
}

Internal classes are visible only within assembly they are defined (well, there is attribute InternalsVisibleTo which allows other assemblies to see internal classes, but without applying this attribute, class is not visible to other assemblies).

Upvotes: 1

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33391

If you want to use a class Doctor from an assembly BL_Backend in a DAL you must add a reference in DAL to the BL_Backend not vise versa.

Upvotes: 0

Related Questions