Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The type 'X' does not exist in the type 'Y.Z'

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.

like image 945
jqno Avatar asked Jun 05 '12 08:06

jqno


Video Answer


2 Answers

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?

like image 148
Adriano Repetti Avatar answered Sep 28 '22 08:09

Adriano Repetti


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

like image 36
Alex Avatar answered Sep 28 '22 07:09

Alex