Condividi tramite


References and the Imports Statement (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

Aliasname refers to a short name you can use within code to refer to an imported namespace. Namespace is a namespace available through either a project reference, through a definition within the project, or through a previous Imports statement.

A module may contain any number of Imports statements. They must appear after any Option statements, if present, but before any other code.

Note

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.

See Also

Tasks

How to: Add or Remove References By Using the Add Reference Dialog Box

How to: Create and Use Assemblies Using the Command Line (C# and Visual Basic)

Reference

Imports Statement (.NET Namespace and Type)

ControlChars

Microsoft.VisualBasic

Concepts

Namespaces in Visual Basic

Assemblies and the Global Assembly Cache (C# and Visual Basic)