You may receive an error message when you use an invalid namespace in Visual Basic .NET or in Visual Basic 2005 (304260)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

This article was previously published under Q304260

SYMPTOMS

When you compile your application in Visual Basic .NET, you receive the following compiler error message:
The Namespace or type <name> for the Import <import name> cannot be found.
In Visual Studio 2005, you receive the following compiler error message:
Namespace or type specified in the Imports <import name> doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.
Note <name> is the name of the namespace you are trying to import, and <import name> is the fully qualified namespace that you are using in your Imports statement. For example, the code
Imports System.MyNamespace
				
might produce one of the following error messages:
The Namespace or type 'MyNamespace' for the Import 'System.MyNamespace' cannot be found.
Namespace or type specified in the Imports System.MyNamespace doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.

CAUSE

There are two reasons why you might be experiencing this error:
  • You might have misspelled the name of the namespace that you are trying to import. Without the correct name, the compiler is unable to find the namespace.
  • You may have spelled the name correctly, but you do not have the assembly where the namespace resides referenced in your project.

RESOLUTION

To resolve this problem, use the following procedures:
  1. Namespaces are logical groupings of classes that are contained within assemblies. You can think of an assembly as a .dll or .exe file. When you compile a project in Visual Studio .NET, you are creating an assembly. Assemblies can contain multiple namespaces. In order to import a namespace you must first reference the assembly where the namespace resides. To reference the assembly, from the Project menu click Add Reference. On the .NET tab, select the assembly that contains the namespace that you are trying to import, or Browse to the folder that contains the assembly.
  2. Check your imports statement to make sure that you have the correct name of the namespace. Keep in mind that the namespace you want to use might be nested in one or more namespaces. For example, the SqlClient namespace is located in the Data namespace. The Data namespace is located in the System namespace. So, to import the SqlClient namespace you would need to use the following Imports statement:
    Imports System.Data.SqlClient
    					

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Create a new Visual Basic .NET or Visual Basic 2005 console application.
  2. In Module1.vb, at the top of the code file, add the following statement:
    Imports System.Drawing
    					
  3. To see the error message, move the mouse pointer over the Imports statement for the namespace or build the project. Notice that you will receive the error because you have not referenced the System.Drawing.dll assembly. If you reference this assembly the error message will disappear.

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbprb kbProd2Web KB304260 kbAudDeveloper