2

I'm playing around with namespaces and faced a problem I can't really understand.

My namespace structure in a given class library is as follows:

Namespace History
Namespace History.Antiquity
Namespace History.Antiquity.East
Namespace History.Antiquity.West

When inside the source file that declares the namespace History, I can't use any of the types defined under History.Antiquity.East or History.Antiquity.West without fully qualifying them, or without qualifying them starting with "Antiquity".

Example Code:

The following for instance, gives me a compile-time error (assuming the type "Persia" is defined in History.Antiquity.East):

using History.Antiquity;

namespace History
{
    public class foo
    {
        public foo()
        {
            East.Persia.Conquer(); // error!
        }
    }

} 

I need to either fully qualify Persia, or qualify it starting from Antiquity. Alternatively, I can introduce an alias:

using East = History.Antiquity.East;

Actual question:

What I can't seem to understand is why is there a problem with this namespace structure. How can it be that .Net finds it conflicting?

2
  • C# namespaces are crap. Java has the upper hand here. I'm forced to use C# sometimes and I always fully qualify namespaces when I have to refer to them. It just looks clearer in the code. Commented Jul 1, 2011 at 20:52
  • 1
    @Steve, could you elaborate? In what way are C# namespaces so much worse? Commented Jul 1, 2011 at 20:57

4 Answers 4

3

You have to open a namespace in order to see it's types in C#. Types from sub-namesapces are not automatically visible. So, put the following on top of your code file:

using History.Antiquity.East;

Then you can directly access your type as follows:

Persia.Conquer();

By the way, your code samples look like a mixture of VB, C# and C++/CLI... what language are you actually using?

Sign up to request clarification or add additional context in comments.

4 Comments

Accessing Persia that way is no problem. I'd rather however understand why I couldn't qualify a type in a descendent namespace, without having to include a using directive. You however explained why in the first paragraph. Thanks.
BTW, what do you mean a mixture of code styles? I wrote that by hand in here. But I can't see how can that be confused with another language?
In your first piece of code, you write Namespace with a capital N, which is the VB way to write it. And while most of your second code piece is C#, "using namespace SomeThing.SomeThing;" is C++/CLI, not C#.
oh right! It's my c++ background calling and my lack of experience in C#. I should have written in an VS before and paste in here. I will edit the post. Thanks.
1

While using partial namespaces like that is perfectly acceptable in VB, C# wasn't designed to work that way.

1 Comment

Wouldn't know about VB before you told me. But isn't this a common namespace implementation. C++ doesn't require of me to declare descendant namespaces either.
1

The code gives you an error, because it should be just

using History.Antiquity;

But adding this in the code doesn't mean you can refer to the History.Antiquity.East namespace just by East.

What you can do is if you're in the History namespace, you can use Antiquity.East, but usings don't affect this.

1 Comment

Thanks. I now realize I need to "declare" the namespace through a using directive before I can use it. A somewhat annoying limitation if, as was the case, I would like to qualify my types in such a way as above.
0
namespace History
{
    using Antiquity;

    public class foo {
        public foo() {
            East.Persia.Conquer(); // error!
        }
    }
}

You could do something like this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.