5

Our team has recently migrated from Visual Studio 2008/.NET3.5 to Visual Studio 2010/.NET4.0. Now, VS2010 gives me a strange error message. It's reproducible with the following program:

using System;

namespace Some.Main
{
}

namespace SomeLib
{
    interface Some
    {
    }
}

namespace ConsoleApplication1
{
    using Some.Main;
    using SomeLib;

    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Press enter to continue");
            Console.ReadLine();
        }
    }
}

This worked just fine in VS2008, but in VS2010 I get the following error message:

The type name 'Main' does not exist in the type 'SomeLib.Some'

Interestingly, if I hit 'Build Solution', the program builds just fine, and I can even execute it without any problems. It's just Visual Studio that seems to have a problem with this code.

Unfortunately, I'm working on a large-ish legacy application and I cannot (easily) change the names of these namespaces.

I'd like to know how I can fix this error, and I'm also curious what causes it.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
jqno
  • 15,133
  • 7
  • 57
  • 84

3 Answers3

9

You simply make the editor confused. Some is both a namespace and an interface name, evidently it doesn't check/parse usings in the order they're declared.

If you want to make clear you're referring to the namespace and not the type name simply add global:: to the using declaration (to start from the root namespace), like this:

using global::Some.Main;

UPDATE
Very good post here on SO linked by @alex in a comment: Should 'using' statements be inside or outside the namespace?

Community
  • 1
  • 1
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
4

I think IntelliSense is confused by the naming and doesn't "understand" who's who.

Attached screenshot demonstrates what's going on: IntelliSense "thinks" Some.Main refers to the interface named Some, probably because there isn't a namespace called Some anywhere.

Luckily, the compiler isn't fooled like that and the code seems to work just fine, like you said.

IntelliSense being fooled

Alex
  • 23,004
  • 4
  • 39
  • 73
  • And after you do a build, IntelliSense stops complaining too. – H H Jun 05 '12 at 09:14
  • @HenkHolterman Until you make a change; then it comes back :). – jqno Jun 05 '12 at 09:17
  • @HenkHolterman And that's more funny, I tried building it, it compiles. Yet it has error when you make changes to code. Are the Visual Studio IDE team and C# compiler team a separate team? – Michael Buen Jun 05 '12 at 09:18
  • 1
    @MichaelBuen I guess they're but more than that they use pretty different techniques to parse code. For Intellisense they key point is speed and error-tolerance. – Adriano Repetti Jun 05 '12 at 09:34
1

Try this:

using Some.Main;
using SomeLib;

namespace ConsoleApplication1
{
    // instead of placing usings here.
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • 2
    Beware, moving `using`s around might lead to code breaking and should *not* be done blindly! See http://stackoverflow.com/questions/125319/should-usings-be-inside-or-outside-the-namespace – Alex Jun 05 '12 at 09:45
  • @alex: however I always place using out of namespace and never hear any problems. – abatishchev Jun 05 '12 at 10:36