Visual Basic Language Reference  

Imports Statement

Imports namespace names from referenced projects and assemblies. Also imports namespace names defined within the same project as the file in which the statement appears.

Imports [ aliasname = ] namespace [ . element ]

Parts

aliasname
Optional. The name by which namespace may also be known or referred to. When the Imports statement does not include aliasname, the elements defined within the specified namespace can be accessed within the file without qualification. When aliasname is specified, it must be used as a qualifier for the names contained in the namespace. Aliases are useful when you need to use items with the same name that are declared in one or more namespaces.
namespace
Required. The name of the namespace being imported. The namespace can be any number of nesting levels deep.
element
Optional. The name of an element declared in the namespace. The element can be an enumeration, structure, class, or module.

Remarks

Each file can contain any number of Imports statements. Imports statements must be placed before any declarations, including Module or Class statements, and before any references to identifiers.

The scope of the elements made available by an Imports statement depends on how specific you are when using the Imports statement. For example, if only a namespace is specified, all uniquely named members of that namespace, and members of modules within that namespace, are available without qualification. If both a namespace and the name of an element of that namespace are specified, only the members of that element are available without qualification.

It is not permitted to define a member at module level with the same name as an import alias.

Example

The following example imports the Microsoft.VisualBasic.Strings class and assigns an alias, Str, that can be used to access the Left method.

Imports Str = Microsoft.VisualBasic.Strings
' Place Imports statements at the top of your program
Class MyClass1
   Sub ShowHello()
      MsgBox(Str.Left("Hello World", 5)) ' Displays the word "Hello"
   End Sub
End Class

See Also

Introduction to the .NET Framework Class Library in Visual Studio | Namespace Statement | Namespaces | Visual Basic .NET and the .NET Framework