Share via


Declaração Imports (Tipo e Namespace .NET)

Permite digitar nomes a ser referenciado sem qualificação de namespace .

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 Nomes de elementos declarados (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.

Comentários

O Imports dedemonstrativo permite que os tipos contidos em um determinado namespace para ser referenciado diretamente.

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 Referências a elementos declarados (Visual Basic).

ObservaçãoObservação

Você pode definir implícito Imports declarações usando a Referências de página, Designer de projeto (Visual Basic). For more information, see Como: Adicionar ou remover Namespaces (Visual Basic) importados.

Import Aliases

Um importar alias define o alias para um namespace ou tipo. Import aliases are useful when you need to use items with the same name that are declared in one or more namespaces. Para obter mais informações e um exemplo, consulte "Um Nomede elemento de qualificação" in Referências a elementos declarados (Visual Basic).

Você não deve declarar um membro no nível de módulo com o mesmo nome como 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.

Nomes de elemento

Se você fornecer element, ele deve representar uma o elemento decontêiner, ou seja, um elemento de programação que pode conter outros elementos. Container elements include classes, structures, modules, interfaces, and enumerations.

O escopo dos elementos disponibilizados por um Imports demonstrativo depende de você especificar 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.

Exemplo

O exemplo a seguir retorna todas as pastas no diretório C:\ usando o deDirectoryInfoclasse.

O código tem não Imports instruções na parte superior do arquivo. Portanto, o DirectoryInfo, StringBuilder, e CrLf referências são inteiramente qualificadas com 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

O exemplo a seguir inclui Imports instruções para namespaces referenciado. Portanto, os tipos não precisam ser totalmente qualificado com os 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

O exemplo a seguir inclui Imports instruções que criar aliases para os namespaces referenciado. Os tipos são qualificados com os 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

O exemplo a seguir inclui Imports instruções que criar aliases para os tipos referenciados. Os aliases são usados para especificar os tipos.

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

Consulte também

Referência

Biblioteca de classes .NET Framework

Instrução Namespace

Instrução Imports (Namespace XML)

Conceitos

Namespaces no Visual Basic

Referências e a instrução Imports (Visual Basic)

Outros recursos

Referências a elementos declarados (Visual Basic)

Histórico de alterações

Date

History

Motivo

Março de 2011

Exemplos adicionais.

Comentários do cliente.

Março de 2011

Adicionadas informações para os comentários.

Aprimoramento de informações.