Visual Basic 2005 with .NET 3.0 Programmer's Reference

You can create new namespaces nested within the root namespace to further categorize your code. The easiest way to create a namespace is by using the Namespace statement. The following code declares a namespace called SchedulingClasses. It includes the definition of the TimeSlot class and possibly other classes.

Namespace SchedulingClasses Public Class TimeSlot ... End Class ... End Namespace

After defining this namespace, code inside the namespace can refer to the new class as simply TimeSlot. Code outside of the namespace can refer to the class using the namespace as shown in the following code (assuming MyApplication is the project’s root namespace):

Dim time_slot As New MyApplication.SchedulingClasses.TimeSlot

You can nest namespaces within other namespaces to any depth. In fact, because all of your application’s code is contained within the root namespace, any namespace you create must be contained within another namespace. There is no way to make a namespace that is not contained within the root namespace.

If you want to make a namespace that lies outside of the application’s root namespace, you must create a library project. Then the code in that project lies within its own root namespace.

The following code defines the DispatchClasses namespace. That namespace contains the Appointment Classes and JobClasses namespaces, each of which defines some classes.

Namespace DispatchClasses Namespace AppointmentClasses Public Class AppointmentWindow ... End Class ... End Namespace Namespace JobClasses Public Class SkilledJob ... End Class ... End Namespace End Namespace

The following code shows how an application could create references to AppointmentWindow and SkilledJob objects using the class’s fully qualified names:

Dim appt As New MyApplication.DispatchClasses.AppointmentClasses.AppointmentWindow Dim job As New MyApplication.DispatchClasses.JobClasses.SkilledJob

A Namespace statement can only appear at the namespace level. You cannot create a namespace within a module, class, or structure.

Inside a namespace, you can define other namespaces, classes, structures, modules, enumerated types, and interfaces. You cannot directly define variables, properties, subroutines, functions, or events. Those items must be contained within some other entity (such as a class, structure, module, or interface).

You can use more than one Namespace statement to define pieces of the same namespace. For example, the following code uses a Namespace statement to make the OrderEntryClasses namespace, and it defines the Employee class inside it. Later, the code uses another Namespace statement to add the Customer class to the same namespace. In this case, the single namespace contains both classes.

Namespace OrderEntryClasses Public Class Employee ... End Class End Namespace ... Namespace OrderEntryClasses Public Class Customer ... End Class End Namespace

Scattering pieces of a namespace throughout your code will probably confuse other developers. One case where it might make sense to break a namespace into pieces would be if you want to put different classes in different code files, either to prevent any one file from becoming too big or to allow different programmers to work on the files at the same time. In that case, it might make sense to place related pieces of the application in the same namespace but in different files.

Категории