MAKING NAMESPACES

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

Code inside the namespace can refer to the TimeSlot 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 is already 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.

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

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

Example program NamespaceHierarchy, which is available for download on the book’s website, defines several nested namespaces.

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.

..................Content has been hidden....................

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