I am creating a little Math library for myself contained within a single project and am running into some issues with namespaces. I have the project MyMathLib and the top level namespace:
namespace MyMathLib
{ ... }
and in a separate file…
namespace MyMathLib.Addition
{ ... }
and…
namespace MyMathLib.Subtraction
{ ... }
In the MyMathLib.Subtraction namespace I have a method that needs to use a static method SomeClass.Work() defined in MyMathLib.Addition so I included using MyMathLib.Addition at the beginning of the Subtraction file. But when I try to use the method it would like me to first qualify it with Addition.SomeClass.Work() and I want to be able to just type SomeClass.Work(). What am I doing wrong?
Thanks!
EDIT
Thanks for the suggestions! In each file, I actually named the class after the namespace (i.e. in the namespace MyMathLib.Addition is a static class Addition and in MyMathLib.Subtraction there is a static class Subtraction). Apparently this is what caused the issue (looking back, I should have stated this instead of using SomeClass). If I change the namespace to MyMathLib.MyAddition while keeping the static class as Addition, the using MyMathLib.MyAddition works as I want; that is, I can now just type Addition.Work() in my static Subtraction class. I’ve seen classes named the same as it’s containing namespace before, could someone maybe explain why this is causing an issue? Shouldn’t the compiler be able to determine whether I want to use the namespace or the class from the context of the code?
I’m guessing that you either have two classes called
SomeClassthat are both in namespaces you reference, or you have a variable or property namedSomeClass. Either of these situations would make it impossible for the compiler to know that you’re trying to call the staticMyMathLib.Addition.SomeClass.Work()method, but the specific solution the compiler is suggesting makes it seem more likely to be the former.Update
Seeing your edit, that makes sense. If you were using these in a namespace outside of
MyMathLib, then you would still be able to avoid this namespace conflict. However, because you are inside theMyMathLib.Subtractionnamespace, the compiler will implicitly consider any portion of the namespace "above" you to take precedence over class names. In this case, when you say "Addition", the compiler will look for the following items to resolve the name:using ... = ...directive.MyMathLib.Subtraction.Additionnamespace.MyMathLib.Additionnamespace.Additionnamespace.usingstatements.In this case, you’re hitting #3 before #4, so you should be able to work around it either by renaming the class or namespace, or by using Yahia’s suggestion (#1):
Update 2
After looking at the article you linked to, it sounds like the explicit
usingstatement still won’t work. I guess item #1 actually gets evaluated down around item #4 instead. Bummer. You can use an alias to give the class a different name locally:But the best solution is still probably just to avoid the namespace collision entirely by changing your namespace or class name.