This documentation is archived and is not being maintained.

6.3 Imports Statement

Visual Studio .NET 2003

Imports statements import the names of entities into a source file, allowing the names to be referenced without qualification.

Within member declarations in a source file that contains an Imports statement, the types contained in the given namespace can be referenced directly, as seen in the following example:

Imports N1.N2

Namespace N1.N2
    Class A
    End Class
End Namespace 

Namespace N3
    Class B
        Inherits A
    End Class
End Namespace 

Here, within the source file, the type members of namespace N1.N2 are directly available, and thus class N3.B derives from class N1.N2.A.

Imports statements must appear after any Option statements but before any type declarations. The compilation environment may also define implicit Imports statements.

Imports statements make names available in a source file, but do not declare anything in the global namespace's declaration space. The scope of the names imported by an Imports statement extends over the namespace member declarations contained in the source file. The scope of an Imports statement specifically does not include other Imports statements, nor does it include other source files. Imports statements may not refer to one another.

In this example, the last Imports statement is in error because it is not affected by the first import alias.

Imports R1 = N1 ' OK.
Imports R2 = N1.N2 ' OK.
Imports R3 = R1.N2 ' Error: Can't refer to R1.

Namespace N1.N2
End Namespace 
Note   The namespace or type names that appear in Imports statements are always treated as if they are fully qualified. That is, the leftmost identifier in a namespace or type name always resolves in the global namespace and the rest of the resolution proceeds according to normal name resolution rules. This is the only place in the language that applies such a rule; the rule ensures that a name cannot be completely hidden from qualification. Without the rule, if a name in the global namespace were hidden in a particular source file, it would be impossible to specify any names from that namespace in a qualified way.

In this example, the Imports statement always refers to the global System namespace, and not the class in the source file.

Imports System   ' Imports the namespace, not the class.

Class System
End Class
ImportsStatement ::= Imports ImportsClauses LineTerminator
ImportsClauses ::=
   ImportsClause |
   ImportsClauses , ImportsClause
ImportsClause ::= ImportsAliasClause | ImportsNamespaceClause

See Also

6.3.1 Import Aliases | 6.3.2 Namespace Imports | 6.2 Compilation Options |6.4 Namespaces | Imports Statement (Visual Basic Language Reference) | References and the Imports Statement (Visual Basic Language Concepts)