views:

92

answers:

4

I have a webservice project with a class (let's refer to it as webservice.classA).

I have another class project producing a dll which references that class in its own namespace and instantiates an instance of it (lets call the dlls namespace dllnamespace).

In another project I want to access the member in the dll

e.g.

using webservice;

namespace other_project
{
    class B
    {
        classA copy = null;
        //....
        dllnamespace.dostuff(); // amongst other things instantiates a classA object
        //....
        copy = dllnamespace.getclassA(); // method to return classA member

The compiler error I get is cannot convert type from dllnamespace.webservice.classA to other_project.webservice.classA

I guess I have a fundamental design flaw but I figure there must be (?) a way to declare/use "webservice.classA" in more than one namespace.

+1  A: 

Change the copy definition line to:

dllnamespace.webservice.classA copy = null;
Philip Smith
+2  A: 

You have a name clash. The supported way of avoiding this (short of not naming your classes the same), is to define a using alias for one of the classes:

using webservice.classA = myWebserviceClassA;
driis
@driis : I am not sure whether the alias on the RHS is correct?!?.
Shankar Ramachandran
@Shankar, it is supported to declare an alias this way. Feel free to check with the compiler :-) (And by the way, it corresponds to the 2nd example of the link in the answer).
driis
@driis, I know this borders on nitpicking :-) ... but just checked in my VS 2008 ... having the alias on the right hand side does give a compiler error. Assuming -myWeberviceClassA -...appears on the ..left hand side of the equation... :-) ... it is correct.
Shankar Ramachandran
A: 

That's just the problem - you cannot have a class in more than one namespace. This is what namespaces were designed for - to prevent classes with the same name written by different people from aliasing. You'll need to decide for one of your namespaces to own that class and in the other one to import it. Alternatively if the dll and the web service are part of the same distributed app then they should use the same namespace.

abc
I'm pretty new (bleedin' obvious) to namespaces. I suspect you are correct and that in this case it would make sense for the "other" project to have the same namespace.
pyb
+2  A: 

You are right...the design flaw does exist in terms of naming.

Let us assume:

  • you have a class named MyClass

  • the class exists both in namespace- abc.xyz.qwe.tyu.MyClass

  • and in namespace - sed.qwe.dfg.ert.MyClass

The workaround is -

using NS1 = abc.xyz.qwe.tyu.MyClass;
using NS2 = sed.qwe.dfg.ert.MyClass;

This way you avoid the clash. Also, helpful to use if you have very long namespaces.

FURTHER REFERENCE : (From MSDN article on using Directive )

  • The scope of a using directive is limited to the file in which it appears.

  • Create a using alias to make it easier to qualify an identifier to a namespace or type.

  • Create a using directive to use the types in a namespace without having to specify the namespace. A using directive does not give you access to any namespaces that are nested in the namespace you specify.

Shankar Ramachandran