Поделиться через


Imports Statement (.NET Namespace and Type)

Enables type names to be referenced without namespace qualification.

Imports [ aliasname = ] namespace
-or-
Imports [ aliasname = ] namespace.element

Parts

Term

Definition

aliasname

Optional. An import alias or name by which code can refer to namespace instead of the full qualification string. See Declared Element Names (Visual Basic).

namespace

Required. The fully qualified name of the namespace being imported. Can be a string of namespaces nested to any level.

element

Optional. The name of a programming element declared in the namespace. Can be any container element.

Remarks

The Imports statement enables types that are contained in a given namespace to be referenced directly.

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.

Imports System.Collections.Generic

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.

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 References to Declared Elements (Visual Basic).

Note

You can define implicit Imports statements by using the References Page, Project Designer (Visual Basic). For more information, see How to: Add or Remove Imported Namespaces (Visual Basic).

Import Aliases

An import alias defines the alias for a namespace or type. 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 "Qualifying an Element Name" in References to Declared Elements (Visual Basic).

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.

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.

Element Names

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.

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.

Example

The following example returns all the folders in the C:\ directory by using the DirectoryInfo class.

The code has no Imports statements at the top of the file. Therefore, the DirectoryInfo, StringBuilder, and CrLf references are all fully qualified with the namespaces.

Public Function GetFolders() As String 
    ' Create a new StringBuilder, which is used 
    ' to efficiently build strings. 
    Dim sb As New System.Text.StringBuilder

    Dim dInfo As New System.IO.DirectoryInfo("c:\")

    ' Obtain an array of directories, and iterate through 
    ' the array. 
    For Each dir As System.IO.DirectoryInfo In dInfo.GetDirectories()
        sb.Append(dir.Name)
        sb.Append(Microsoft.VisualBasic.ControlChars.CrLf)
    Next 

    Return sb.ToString
End Function

The following example includes Imports statements for the referenced namespaces. Therefore, the types do not have to be fully qualified with the namespaces.

' Place Imports statements at the top of your program. 
Imports System.Text
Imports System.IO
Imports Microsoft.VisualBasic.ControlChars
Public Function GetFolders() As String 
    Dim sb As New StringBuilder

    Dim dInfo As New DirectoryInfo("c:\")
    For Each dir As DirectoryInfo In dInfo.GetDirectories()
        sb.Append(dir.Name)
        sb.Append(CrLf)
    Next 

    Return sb.ToString
End Function

The following example includes Imports statements that create aliases for the referenced namespaces. The types are qualified with the aliases.

Imports systxt = System.Text
Imports sysio = System.IO
Imports ch = Microsoft.VisualBasic.ControlChars
Public Function GetFolders() As String 
    Dim sb As New systxt.StringBuilder

    Dim dInfo As New sysio.DirectoryInfo("c:\")
    For Each dir As sysio.DirectoryInfo In dInfo.GetDirectories()
        sb.Append(dir.Name)
        sb.Append(ch.CrLf)
    Next 

    Return sb.ToString
End Function

The following example includes Imports statements that create aliases for the referenced types. Aliases are used to specify the types.

Imports strbld = System.Text.StringBuilder
Imports dirinf = System.IO.DirectoryInfo
Public Function GetFolders() As String 
    Dim sb As New strbld

    Dim dInfo As New dirinf("c:\")
    For Each dir As dirinf In dInfo.GetDirectories()
        sb.Append(dir.Name)
        sb.Append(ControlChars.CrLf)
    Next 

    Return sb.ToString
End Function

See Also

Reference

Namespace Statement

Imports Statement (XML Namespace)

Concepts

Namespaces in Visual Basic

References and the Imports Statement (Visual Basic)

References to Declared Elements (Visual Basic)