|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
Imports Statement (.NET Namespace and Type)
Imports namespaces or programming elements defined in the current project or in referenced projects and assemblies.
Each source file can contain any number of Imports statements. These must follow any option declarations, such as the Option Strict statement, and they must precede any programming element declarations, such as Module or Class statements.
You can use Imports only at file level. This means the declaration context for importation must be a source file, and cannot be a namespace, class, structure, module, interface, procedure, or block.
Import aliases are useful when you need to use items with the same name that are declared in one or more namespaces. For more information and an example, see "Classes with the Same Name" in Resolving a Reference When Multiple Variables Have the Same Name.
Note that the Imports statement does not make elements from other projects and assemblies available to your project. Importing does not take the place of setting a reference. It only removes the need to qualify names that are already available to your project. For more information, see "Importing Containing Elements" in Resolving a Reference When Multiple Variables Have the Same Name.
Although the syntax used for declaring an import alias is like that used for importing an XML namespace prefix, the results are different. An import alias can be used as an expression in your code, whereas an XML namespace prefix can be used only in XML literals or XML axis properties as the prefix for a qualified element or attribute name.
Alias Name. You should not declare a member at module level with the same name as aliasname. If you do, the Visual Basic compiler uses aliasname only for the declared member and no longer recognizes it as an import alias.
Namespace Name. You can supply a single namespace name or a string of nested namespaces. Each nested namespace is separated from the next higher level namespace by a period (.), as the following example illustrates.
Element Type. If you supply element, it must represent a container element, that is, a programming element that can contain other elements. Container elements include classes, structures, modules, interfaces, and enumerations.
Scope. The scope of the elements made available by an Imports statement depends on whether you specify element. If you specify only namespace, all uniquely named members of that namespace, and members of container elements within that namespace, are available without qualification. If you specify both namespace and element, only the members of that element are available without qualification.
Qualification. Code outside a namespace or container element must normally qualify a member's name with the name of that namespace of container element. The Imports statement makes such qualification unnecessary unless your project has access to another member with the same name. In such a case you can specify an aliasname in each Imports statement. Then you need only the import aliases to qualify the members with the same name.
Notice that the preceding example imports a nested namespace, Strings within VisualBasic within Microsoft. When the MsgBox Function (Visual Basic) accesses the Left method, it can use the alias str instead of the entire qualification string Microsoft.VisualBasic.Strings.