This documentation is archived and is not being maintained.

6.3.2 Namespace Imports

Visual Studio .NET 2003

A namespace import imports all of the members of a namespace or type, allowing the identifier of each member of the namespace or type to be used without qualification. In the case of types, a namespace import only allows access to the shared members of the type without requiring qualification of the class name. In particular, it allows the members of enumerated types to be used without qualification. For example:

Imports Colors

Enum Colors
    Red
    Green
    Blue
End Enum

Module M1
    Sub Main()
        Dim c As Colors = Red
    End Sub
End Module

Unlike an import alias, a namespace import has no restrictions on the names it imports and may import namespaces and types whose identifiers are already declared within the global namespace. The names imported by a regular import are shadowed by import aliases and declarations in the source file.

In the following example, A refers to N3.A rather than N1.N2.A within member declarations in the N3 namespace.

Imports N1.N2

Namespace N1.N2
    Class A
    End Class
End Namespace

Namespace N3
    Class A
    End Class

    Class B
        Inherits A
    End Class
End Namespace 

When more than one imported namespace contains members by the same name (and that name is not otherwise shadowed by an import alias or declaration), a reference to that name is ambiguous and causes a compile-time error.

Imports N1
Imports N2

Namespace N1
    Class A
    End Class
End Namespace 

Namespace N2
    Class A
    End Class
End Namespace 

Namespace N3
    Class B
        Inherits A ' Error, A is ambiguous.
    End Class
End Namespace

In the above example, both N1 and N2 contain a member A. Because N3 imports both, referencing A in N3 causes a compile-time error. In this situation, the conflict can be resolved either through qualification of references to A, or by introducing an import alias that picks a particular A, as in the following example:

Imports N1
Imports N2
Imports A = N1.A

Namespace N3
    Class B
        Inherits A ' A means N1.A.
    End Class
End Namespace 

Only namespaces, classes, structures, enumerated types, and standard modules may be imported.

ImportsNamespaceClause ::= NamespaceOrTypeName

See Also

6.3.1 Import Aliases | 6.3 Imports Statement | Imports Statement (Visual Basic Language Reference) | References and the Imports Statement (Visual Basic Language Concepts)

Show: