Compiler error occurs when you duplicate namespace names (316734)



The information in this article applies to:

  • Microsoft Visual Basic 2005 Express Edition
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# 2005, Express Edition
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

This article was previously published under Q316734

SYMPTOMS

When you compile your project, you may receive a compiler error that states that a method or a property does not exist in a specific namespace.

You may notice that the namespace that the error refers to is a namespace that you created, not the common language runtime namespace that you intended to use.

CAUSE

This behavior occurs when you name a custom namespace by using the same name as one of the common language runtime namespaces.

RESOLUTION

When you choose names for the custom namespaces in your project, make sure that you do not duplicate namespaces that already exist in the common language runtime or in other referenced assemblies. If you duplicate namespace names, code in your namespace resolves to your custom namespace and not to the namespace in the common language runtime.

If you cannot rename your custom namespace, you can use an alias for that namespace. For more information about how to create an alias for your custom namespace, see the steps in the "More Information" section.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to reproduce the behavior

Visual C# 2005 or Visual C# .NET

  1. Open a new Console Application project in Microsoft Visual C# 2005 or in Microsoft Visual C# .NET.
  2. Add a new class to the project.
  3. Replace the code in Class2.cs with the following code.

    Note In Visual C# 2005, the default file is Program.cs.
    using System;
    namespace Test.System
    {
    	class X 
    	{
    	}
    }
    					
  4. Add another new class to the project.
  5. Replace the code in Class3.cs with the following code.

    Note In Visual C# 2005, the new file is Class1.cs.
    using System;
    namespace Test.Lists
    {
    	 
    	class Y
    	{
    		public static void test()
    		{
    			System.Console.WriteLine("Hello");	
    		}
    	}
    }
    					
  6. Build the project. You receive the following error message:
    The type or namespace name 'Console' does not exist in the class or
    namespace 'Test.System' (are you missing an assembly reference?)
  7. To create an alias for this custom namespace, use the following code.
    using  MySystem = System;
    namespace Test.Lists
    {
    	 
        class Y
        {
    	public static void test()
    	{
    	    MySystem.Console.WriteLine("Hello");	
    	}
        }
    }
    					

Visual Basic 2005 or Visual Basic .NET

  1. Open a new Console Application project in Microsoft Visual Basic 2005 or in Microsoft Visual Basic .NET.
  2. Add a new class to the project.
  3. Replace the code in Class1.vb with the following code.
    Namespace Test.System
    
        Class X
        End Class
    End Namespace
    					
  4. Add another new class to the project.
  5. Replace the code in Class2.vb with the following code.
    Imports System
    Namespace Test.Lists
    
        Class Y
            Public Sub test()
                System.Console.WriteLine("Hello")
            End Sub
        End Class
    
    End Namespace
    					
  6. Build the project. You receive the following error message:
    'Console' is not a member of 'System'
  7. To create an alias for this custom namespace, use the following code.
    Imports MySystem = System
    Namespace Test.Lists
    
        Class Y
            Public Sub test()
                MySystem.Console.WriteLine("Hello")
            End Sub
        End Class
    End Namespace
    					

Modification Type:MajorLast Reviewed:3/17/2006
Keywords:kbtshoot kbvs2005applies kbvs2005swept kbCompiler kbprb KB316734