Imports namespaces or programming elements defined in referenced projects and assemblies. Also imports namespaces or elements defined within the same project.
Optional. An import alias or name by which code can refer to namespace instead of the full qualification string. See Declared Element Names.
Required. The fully qualified name of the namespace being imported. Can be a string of namespaces nested to any level.
Optional. The name of a programming element declared in the namespace. Can be any container element.
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.
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.
' Place Imports statements at the top of your program. Imports str = Microsoft.VisualBasic.Strings
Class testClass1 Sub showHello() ' Display only the word "Hello" MsgBox(str.Left("Hello World", 5)) End Sub End Class
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.