Referências e a instrução Imports (Visual Basic)
You can make external objects available to your project by choosing the Add Reference command on the Project menu. References in Visual Basic can point to assemblies, which are like type libraries but contain more information.
The Imports Statement
Assemblies include one or more namespaces. When you add a reference to an assembly, you can also add an Imports statement to a module that controls the visibility of that assembly's namespaces within the module. The Imports statement provides a scoping context that lets you use only the portion of the namespace necessary to supply a unique reference.
The Imports statement has the following syntax:
Imports [| Aliasname =] Namespace
Aliasnamerefere-se para um nome curto, que você pode usar para se referir a um namespacede importado dentro do código. Namespaceé uma referência de projeto , por meio de uma definição de dentro do projetoou a versão anterior de um namespace disponíveis por meio de um Importsdemonstrativo.
A module may contain any number of Imports statements. They must appear after any Option statements, if present, but before any other code.
Observação |
---|
Do not confuse project references with the Imports statement or the Declare statement. Project references make external objects, such as objects in assemblies, available to Visual Basic projects. The Imports statement is used to simplify access to project references, but does not provide access to these objects. The Declare statement is used to declare a reference to an external procedure in a dynamic-link library (DLL). |
Using Aliases with the Imports Statement
The Imports statement makes it easier to access methods of classes by eliminating the need to explicitly type the fully qualified names of references. Aliases let you assign a friendlier name to just one part of a namespace. For example, the carriage return/line feed sequence that causes a single piece of text to be displayed on multiple lines is part of the ControlChars module in the Microsoft.VisualBasic namespace. To use this constant in a program without an alias, you would need to type the following code:
MsgBox("Some text" & Microsoft.VisualBasic.ControlChars.CrLf &
"Some more text")
Imports statements must always be the first lines immediately following any Option statements in a module. The following code fragment shows how to import and assign an alias to the Microsoft.VisualBasic.ControlChars module:
Imports CtrlChrs = Microsoft.VisualBasic.ControlChars
Future references to this namespace can be considerably shorter:
MsgBox("Some text" & CtrlChrs.CrLf & "Some more text")
If an Imports statement does not include an alias name, elements defined within the imported namespace can be used in the module without qualification. If the alias name is specified, it must be used as a qualifier for names contained within that namespace.
Consulte também
Tarefas
Como: Adicionar ou remover referências no Visual Studio
Como: Criar e usar Assemblies usando a linha de comando (C# e Visual Basic)
Referência
Declaração Imports (Tipo e Namespace .NET)