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, ...

Get Visual Basic 2012 Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.