5

This is a little spooky.

I'm thinking there must be a setting somewhere that explains why this is happening.

In our solution there are about 50 different projects. For the most part, the libraries start with the namespace OurCompany.

We have OurComany.This.That and OurCompany.Foo.Bar... etc.

There is a namespace/class clash between an external library with the namespace

OurCompany.Foo.Bar

And a class that is qualified like so..

OurCompany.Some.Location.Foo

The error goes like this:

Error   75  The type or namespace name 'MethodName' does not exist in the
namespace 'OurCompany.Foo' (are you missing an assembly reference?)

Even Resharper gives me a "Qualifier is redundant" message when I fully qualify anything under the "OurCompany" namespace.. i.e.

OurCompany.Some.Location.Foo.MethodName();
//OurCompany is redundant

I cannot figure out what on earth is doing this. The solution is pretty huge, so ripping things apart to try and reverse engineer the problem is not a great solution for me.

I should state that if I use...

Some.Location.Foo.MethodName(); //Leaving out OurCompany

...the Resharper message goes away.

Nicholas
  • 3,286
  • 5
  • 27
  • 35
  • Does `SomeStaticClass.SomeStaticMethod();` work? What is the code that causes the error message? – Cyral Jun 17 '15 at 01:03
  • if you are in class `OurCompany`. you dont have to Qualify it. just type `SomeStaticClass.SomeStaticMethod();` or even `SomeStaticMethod(); `. it depends where you are.thats why resharper is saying – M.kazem Akhgary Jun 17 '15 at 01:04
  • It's happening throughout the solution. I have 3122 errors. Nothing works because it won't compile. – Nicholas Jun 17 '15 at 01:05
  • the redundant warning is not error.you have 3122 errors i believe that you need to reference some files to your project in order to work. – M.kazem Akhgary Jun 17 '15 at 01:14
  • No, I'm sorry that's definitely not it. Before I added my external library with the namespace "Foo", everything compiled fine. The compiler is going crazy on anything that uses the "Foo" class in the main solution because of the clash with the "Foo" namespace. This is nuts though, because Foo isn't in the same scope. The only thing I have to go on is that the First namespace "OurCompany", is considered redundant throughout the solution. – Nicholas Jun 17 '15 at 01:18
  • Guys, I thank you for your help, but you are ignoring the details of the question. If you have some advice for making it more clear I would be happy to do so, but the real problem here is that the first namespace qualifier "OurCompany", is considered redundant in classes through out the entire solution. – Nicholas Jun 17 '15 at 01:23
  • for us to explain what is happening, we would need to see the full list of `using` statements, and the `namespace` declaration that the code you are having trouble with is in. – Claies Jun 17 '15 at 01:35
  • Ok.. thanks, but there are hundreds of namespaces and thousands of errors right now. – Nicholas Jun 17 '15 at 01:46

2 Answers2

2

I thought I understood what was happening here, but now I'm seeing some weird behavior that's making me question my understanding of C#'s namespace scoping behavior.

Obviously, the basic issue is scoping. Presumably, you're working on something in some namespace under OurCompany; let's just say for sake of argument, you're in OurCompany.This.That. Automatically, any types or namespaces found directly in the OurCompany.This.That, OurCompany.This, and OurCompany namespaces are in scope, no usings required. This is why things broke as soon as an assembly with an OurCompany.Foo namespace got involved (everything in OurCompany is in scope by default, including the Foo namespace, and namespaces [apparently] take priority) and also why you're getting the redundant namespace warnings (the Some namespace is defined in the OurCompany namespace, so it's automatically in scope).

But trying to repro this behavior, I encountered something strange. I created one file to hold the rest of the relevant world:

namespace OurCompany
{
    namespace Some
    {
        namespace Location
        {
            public class Foo
            {
                public static void MethodName() { }
            }
        }
    }

    namespace Foo
    {
        namespace Bar { }
    }
}

And found that the following (which I gather is similar to what you're doing) doesn't work:

using OurCompany.Some.Location;

namespace OurCompany
{
    namespace This
    {
        namespace That
        {
            class BeepBoop
            {
                private void DoSomething()
                {
                    Foo.MethodName();  // No good; Foo is a namespace here.
                }
            }
        }
    }
}

...but this did:

namespace OurCompany
{
    namespace This
    {
        namespace That
        {
            using OurCompany.Some.Location;
            class BeepBoop
            {
                private void DoSomething()
                {
                    Foo.MethodName();  // Puh-wha?  This works?
                }
            }
        }
    }
}

I freely admit I don't know what's going on here. But apparently the scoping isn't as simple as "Whatever's in scope is in scope, and namespaces take priority."

Ben Nesson
  • 36
  • 1
  • Whoa... nice experiment. – Nicholas Jun 17 '15 at 02:55
  • I found this link after reading your answer: http://stackoverflow.com/questions/125319/should-using-statements-be-inside-or-outside-the-namespace – Nicholas Jun 17 '15 at 03:04
  • Okay, so the subtlety is that, unlike my initial conception of the scoping as basically just being a big pile that stuff gets added to or removed from, it's more layered. So in the first example, when the compiler looks for `Foo`, it first checks DoSomething. It doesn't find it there, so it moves up a layer to BeepBoop, and so on, until it gets to the `OurCompany` namespace scope. There, it finds a namespace named `Foo`. But in the second example, once the compiler gets to the scope of the `That` namespace, it finds the using statement that brings the Foo class into scope. – Ben Nesson Jun 17 '15 at 19:53
1

I have seen this error whenever my referenced dlls are not built. It is possible that the references are not built and have some build errors. Hence both Resharper and VS are complaining about the types that are missing.

In order to fix it, I would recommend you to do the following:

  1. In the solution of 50 projects try to figure out the base projects. You can use the Project Build Order (right click sln in Solution Explorer)
  2. Disable all other projects and hit build.
  3. Now verify if the build succeeds. If build is not successful, fix the errors. If successful repeat from step 1.

I know this is tedious, but I can see this is a good way to go about fixing the overwhelming (#3000) errors you are seeing.

whihathac
  • 1,741
  • 2
  • 22
  • 38
  • 1
    Thanks for your help. I appreciate the considered reading of my problem. – Nicholas Jun 17 '15 at 01:48
  • Good luck. Do let me know if you see any issues. – whihathac Jun 17 '15 at 01:48
  • Please mark this as an answer if it indeed is a solution for your problem. – whihathac Jun 17 '15 at 17:45
  • 1
    Please read Ben Nesson's answer. He was able to demonstrate the reason for the problem. After seeing that, I was able to find a similar question, and discovered that it was the order of namespace resolution that was causing my issue. After that, it was an easy fix. – Nicholas Jun 17 '15 at 19:08