Class name conflict

Q

qglyirnyfgfo

Suppose that you have two dlls (ClassLibrary1.dll and
ClassLibrary2.dll).

Now suppose that both dlls have the same class declared on them using
the exact same name space and class name, for example, supposed both
dlls have the following snippet of code:

namespace SomeNameSpace
{
public class Class1
{
}
}

Now supposed you reference both dlls into your main project and add
the following snippet of code into this main project.

SomeNameSpace.Class1 cls = new SomeNameSpace.Class1();

If you try to compile the project you will get an error message
stating that “SomeNameSpace.Class1()” exist in both dlls and so the
compiler can’t choose which one to select.

Is there a way to specify what dll to use when instantiating Class1?

Thank you.
 
J

Jon Skeet [C# MVP]

Suppose that you have two dlls (ClassLibrary1.dll and
ClassLibrary2.dll).

Now suppose that both dlls have the same class declared on them using
the exact same name space and class name, for example, supposed both
dlls have the following snippet of code:

namespace SomeNameSpace
{
    public class Class1
    {
    }

}

Now supposed you reference both dlls into your main project and add
the following snippet of code into this main project.

SomeNameSpace.Class1 cls = new SomeNameSpace.Class1();

If you try to compile the project you will get an error message
stating that “SomeNameSpace.Class1()” exist in both dlls and so the
compiler can’t choose which one to select.

Is there a way to specify what dll to use when instantiating Class1?

Yes, assuming you're using C# 2 or later. You need to use a feature
called "extern aliases" - that should be enough to get you going with
a good search. However, I'd stress that if you can *possibly* avoid
this, do so.

Jon
 
J

Jeff Winn

I have to agree with Jon, you should avoid reusing the same namespace and
type name repeatedly.

Suppose that you have two dlls (ClassLibrary1.dll and
ClassLibrary2.dll).

Now suppose that both dlls have the same class declared on them using
the exact same name space and class name, for example, supposed both
dlls have the following snippet of code:

namespace SomeNameSpace
{
public class Class1
{
}

}

Now supposed you reference both dlls into your main project and add
the following snippet of code into this main project.

SomeNameSpace.Class1 cls = new SomeNameSpace.Class1();

If you try to compile the project you will get an error message
stating that “SomeNameSpace.Class1()” exist in both dlls and so the
compiler can’t choose which one to select.

Is there a way to specify what dll to use when instantiating Class1?

Yes, assuming you're using C# 2 or later. You need to use a feature
called "extern aliases" - that should be enough to get you going with
a good search. However, I'd stress that if you can *possibly* avoid
this, do so.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top