Class and namespace with same name

E

_eddie

Easier to show than to explain:

file 1:
---------------------------------------------------
namespace Somename {
public class Somename {
....
}
}

file 2:
--------------------------------------------------
using Somename;
......

Somename s1 = new Somename; // Error!
Somename s2 = new Somename.Somename; // OK
--------------------------------------------------

This generates an error because the compiler thinks it is ambiguous.
It relates to the fact that the namespace and class in file 1 have the
same name. Changing either name fixes it.

Not sure why. You'd think it would be qualified by the 'using'
statement. Is this according to spec? I thought I had seen this done
without the error message.
 
N

n!

Not sure why. You'd think it would be qualified by the 'using'
statement. Is this according to spec? I thought I had seen this done
without the error message.

This is down to the way the language determines what your declaration is
referring to. When looking up a typename, the language checks locally before
trying other areas of the code. As the namespace 'Somename' is within the
namespace you're working in (I guess you're at global scope, in the example)
it finds that first. If you are within an unrelated namespace, it should
fail to find 'Somename' and then proceed to look inside the 'Somename'
namespace (because of your 'using' statement). It will also work if your
code referencing the 'Somename' class is within the 'Somename' namespace, as
it will find the class first in its search :)

Hmmm, I think I made that as clear as mud, sorry.

n!
 
E

_ed

This is down to the way the language determines what your declaration is
referring to. When looking up a typename, the language checks locally before
trying other areas of the code. As the namespace 'Somename' is within the
namespace you're working in (I guess you're at global scope, in the example)
it finds that first. If you are within an unrelated namespace, it should
fail to find 'Somename' and then proceed to look inside the 'Somename'
namespace (because of your 'using' statement). It will also work if your
code referencing the 'Somename' class is within the 'Somename' namespace, as
it will find the class first in its search :)

Hmmm, I think I made that as clear as mud, sorry.

Ha! About as clear as my illustration permitted, I guess. Yes, I
should have clarified that the caller is inside another namespace.

Revised:

file 1:
---------------------------------------------------
namespace Somename {
public class Somename {
....
}
}

file 2:
--------------------------------------------------
using Somename;

namespace Anothername {
class Anotherclass {
public static void Main() {
Somename s1 = new Somename; // Error!
Somename s2 = new Somename.Somename; // OK
}
}
}
--------------------------------------------------

Yes, I do see what's going on; the caller is looking for namespaces
first. It finds one, then (even though that namespace was already
called out in the 'using' statement) it assumes that it is looking at
a namespace ref and not a class ref. It looks no further.

I thought that I had seen code that implied the compiler handled this
more 'intelligently'. I guess not. For the number of silly things
that they *did* do (overloading 'new' and 'using' require analysis of
context), I thought they could handle it.
 

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