Visual Basic Imports Statement

A namespace is a container for a set of related objects. For example, the System.IO namespace, which is part of the .NET framework, provides the facility to read and write to files and data streams, as well as basic file and directory support, such as, checking for the existence of a directory, or, returning the names of files within a particular directory.

As well as the namespaces included in the .NET framework, namespaces can also be user defined.

The ‘Imports’ statement allows the use of the contents of a namespace without, for example, having to include the namespace when checking for the existence of a directory. They are placed at the top of a Visual Basic script. Using the System.IO namespace mentioned above as an example, the ‘Imports’ statement would be as follows.

Imports System.IO

Including this statement means that, for example, an ‘If’ statement checking for the existence of a directory can be written as follows.

If Directory.Exists("C:\Demo") Then

    Console.WriteLine("Directory Exists.")

End If

Without the inclusion of the ‘Imports’ statement, the namespace would have to be included in the ‘If’ statement.

If System.IO.Directory.Exists("C:\Demo") Then

    Console.WriteLine("Directory Exists.")

End If

If the namespace is used a number of times, as in the examples for batch renaming, formatting and merging files, a lot of unnecessary typing is cut out with the ‘Imports’ statement.