THE IMPORTS STATEMENT

Visual Studio defines thousands of variables, classes, routines, and other entities to provide tools for your applications. It categorizes them in namespaces to prevent name collisions and to make it easier for you to find the items you need.

The .NET Framework root namespaces are named Microsoft and System. The Microsoft namespace includes namespaces that support different programming languages and tools. For example, typical namespaces include CSharp, JScript, and VisualBasic, which contain types and other tools that support the C#, JScript, and Visual Basic languages. The Microsoft namespace also includes the Win32 namespace, which provides classes that handle operating system events and that manipulate the registry.

The System namespace contains a huge number of useful programming items, including many nested namespaces. For example, the System.Drawing namespace contains classes related to drawing, System.Data contains classes related to databases, System.Threading holds classes dealing with multithreading, and System.Security includes classes for working with security and cryptography.

Note that these namespaces are not necessarily available to your program at all times. For example, by default, the Microsoft.JScript namespace is not available to Visual Basic programs. To use it, you must first add a reference to the Microsoft.JScript.dll library.

Visual Studio includes so many programming tools that the namespace hierarchy is truly enormous. Namespaces are refined into sub-namespaces, which may be further broken into more namespaces until they reach a manageable size. Although this makes it easier to differentiate among all of the different programming entities, it makes the fully qualified names of some classes rather cumbersome.

Example program DrawDashes, which is available for download on the book’s website, uses the following code to draw a rectangle inside its form. Fully qualified names such as System.Drawing .Drawing2D.DashStyle.DashDotDot are so long that they make the code hard to read.

Private Sub DrawDashedBox(gr As System.Drawing.Graphics)
    gr.Clear(Me.BackColor)
 
    Dim rect As System.Drawing.Rectangle = Me.ClientRectangle
    rect.X += 10
    rect.Y += 10
    rect.Width -= 20
    rect.Height -= 20
 
    Using my_pen As New System.Drawing.Pen(System.Drawing.Color.Blue, 5)
        my_pen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDotDot
        gr.DrawRectangle(my_pen, rect)
    End Using
End Sub

You can use the Imports statement at the top of the file to make using namespaces easier. After you import a namespace, your code can use the items it contains without specifying the namespace.

Example program DrawDashesWithImports, which is also available for download on the book’s website, uses the following code. It imports the System.Drawing and System.Drawing.Drawing2D namespaces so it doesn’t need to mention the namespaces in its object declarations. This version is much easier to read.

Imports System.Drawing
Imports System.Drawing.Drawing2D
...
Private Sub DrawDashedBox(gr As Graphics)
    gr.Clear(Me.BackColor)
 
    Dim rect As Rectangle = Me.ClientRectangle
    rect.X += 10
    rect.Y += 10
    rect.Width -= 20
    rect.Height -= 20
 
    Using my_pen As New Pen(Color.Blue, 5)
        my_pen.DashStyle = DashStyle.DashDotDot
        gr.DrawRectangle(my_pen, rect)
    End Using
End Sub

DRAWING DEFAULTS
System.Drawing is automatically imported by default in Windows Forms applications so you normally don’t need to import it. See the following section for more information on automatic imports.

A file can include any number of Imports statements. The statements must appear at the beginning of the file, and they define namespace shortcuts for the entire file. If you want different pieces of code to use different sets of Imports statements, you must place the pieces of code in different files. If the pieces of code are in the same class, use the Partial keyword so you can split the class into multiple files.


COLLISION PROVISION
If a program imports two namespaces that define classes with the same names, Visual Basic may become confused and give you an ambiguous reference error. To fix the problem, the code must use fully qualified names to select the right versions.
For example, suppose that the Payroll and HumanResources modules both define Employee classes. Then you must use the fully qualified names Payroll.Employee and HumanResources.Employee to differentiate between the two within the same file.

The complete syntax for the Imports statement is as follows:

Imports [alias =] namespace[.element]

Later sections in this chapter describe namespace aliases and elements in detail.

Automatic Imports

Visual Basic lets you quickly import a namespace for all of the modules in a project. In Solution Explorer, double-click My Project. Click the References tab to display the page shown in Figure 24-1.

FIGURE 24-1: Use the My Project References tab to import namespaces for every module in a project.

image

In the Imported namespaces list at the bottom, select the check boxes next to the namespaces that you want to import. The program’s files will be able to use the objects defined in these namespaces without including Imports statements.

This is most useful when most of the program’s modules need to import the same namespaces. Including the Imports statement in the files makes it easier for developers to see which namespaces are available, however, so you might want to do this instead, particularly if you use unusual namespaces.

By default, Visual Basic imports namespaces for the type of application you are building. For example, when you start a Windows Form application, Visual Basic imports the following namespaces:

  • Microsoft.VisualBasic
  • System
  • System.Collections
  • System.Collections.Generic
  • System.Data
  • System.Drawing
  • System.Diagnostics
  • System.Windows.Forms
  • System.Linq
  • System.Xml.Linq
  • System.Threading.Tasks

You can use the upper half of the References property page to manage project references. Use the Add and Remove buttons to add and remove references to libraries.

Click the Unused References button near the top in Figure 24-1 to see a list of libraries that are referenced but not currently used by the project. Before you distribute the program, you can remove the unused references.

Namespace Aliases

You can use the alias clause to define a shorthand notation for the namespace. For instance, the following code imports the System.Drawing.Drawing2D namespace and gives it the alias D2. Later, it uses D2 as shorthand for the fully qualified namespace.

Imports D2 = System.Drawing.Drawing2D
...
Dim dash_style As D2.DashStyle = D2.DashStyle.DashDotDot

This technique is handy if you need to use two namespaces that define different classes with the same name. Normally, if two namespaces define classes with the same name, you must use the fully qualified class names so that Visual Basic can tell them apart. You can use aliases to indicate the namespaces more concisely.

Namespace Elements

In addition to importing a namespace, you can import an element within the namespace. This is particularly useful for enumerated types.

For example, the following code imports the System.Drawing.Drawing2D namespace, which defines the DrawStyle enumeration. It declares the variable dash_style to be of the DashStyle type and sets its value to DashStyle.DashDotDot.

Imports System.Drawing.Drawing2D
...
Dim dash_style As DashStyle = DashStyle.DashDotDot
...

Example program DrawDashesImportsDashStyle, which is available for download on the book’s website, uses the following code to import the System.Drawing.Drawing2D.DashStyle enumeration. That allows it to set the value of my_pen.DashStyle to DashDotDot without needing to specify the name of the enumeration (DashStyle).

Imports System.Drawing.Drawing2D
Imports System.Drawing.Drawing2D.DashStyle
...
my_pen.DashStyle = DashDotDot
...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.26.152