Namespaces

 <  Day Day Up  >  

A namespace is a name that is used to organize related types within an assembly or across assemblies. In most of the examples used in the book so far, not much organization has been needed because an example has typically declared only one or two types. However, consider the .NET Framework, which contains thousands upon thousands of types ”finding a single type among all those types could be quite a chore, especially if you weren't sure what name you were looking for! Namespaces are used to organize and categorize the types in the .NET Framework to make it easier to find the types that might be needed in any particular situation. For example, all the types that are used to create forms are defined under the System.Windows.Forms namespace, while all the types that can be used to do disk I/O are defined in the System.IO namespace. As you can see from the examples just given, namespaces can contain other namespaces, allowing multiple levels of organization.

A new namespace is declared in Visual Basic .NET using a namespace declaration. Any type declaration within a namespace declaration becomes part of that namespace. For example, the following code declares the module Test within the Testing namespace.

 Namespace Testing   Module Test     Sub Main()     End Sub   End Module End Namespace 

Namespace declarations can be nested within one another. The following example defines two modules: Testing.Test and Testing.SubTest.Test .

 Namespace Testing   Module Test     ...   End Module   Namespace SubTest     Module Test       ...     End Module   End Namespace End Namespace 

Instead of nesting two namespace declarations within one another, it is also possible for brevity's sake to use a dot-separated name to refer to all the nested namespace declarations at once. The following two declarations define types in exactly the same namespace.

 Namespace Testing.SubTest   Module Test1     ...   End Module End Namespace Namespace Testing   Namespace SubTest     Module Test2       ...     End Module   End Namespace End Namespace 

Namespaces are unlike any other kind of declaration in that they are open -ended . This means that the same namespace can be declared repeatedly, and each declaration adds members to the same namespace. For example, the following declarations add two modules to the same namespace; thus they can refer to one another without qualification.

 Namespace Testing   Module Test     Sub Main()       Console.WriteLine(Add(10, 20))     End Sub   End Module End Namespace Namespace Testing   Module Math     Function Add(ByVal x As Integer, ByVal y As Integer) As Integer       Return x + y     End Function   End Module End Namespace 

NOTE

Because namespaces are open-ended, the accessibility level Private has no meaning in a namespace. Declarations in namespaces can only use the access levels Public and Friend .


Multiple assemblies can contribute types to a namespace. For example, all the class libraries in the .NET Framework contribute types to the System namespace (or to a subnamespace of the System namespace).

Style

Top-level namespaces are generally reserved for a particular company or broad category of functionality and should not be used when you are defining new types. For example, the top-level namespace System is reserved for types defined by the .NET Framework. Although it is possible to do so, new types should not be defined in the System namespace, to avoid confusion and to avoid name collisions when future versions of the Framework are released.


Fully Qualified Names

As discussed in Chapter 2, Basic Concepts, when a declaration occurs within a container, it is often necessary to use a qualified name (such as System.Collection.ArrayList ) to refer to the declaration. The fully qualified name of a declaration is the name of the declaration qualified with all the namespaces and types that contain it. The fully qualified name of a declaration uniquely identifies it in the same way that a person's full name ("John Q. Smith") distinguishes them from others who may share the same first name or last name. The fully qualified name is useful when we are talking about declarations ”for example, most references to Framework types in this book use the type's fully qualified name.

The following example gives some types and namespaces, and their fully qualified names in comments after the name.

 Namespace A    ' Fully qualified name: A   Class B      ' Fully qualified name: A.B     Class C    ' Fully qualified name: A.B.C     End Class   End Class   Namespace D.E ' Fully qualified name: A.D.E     Class F     ' Fully qualified name: A.D.E.F     End Class   End Namespace End Namespace 

NOTE

Even though a module's name does not have to be used when you are referring to declarations in the module, the fully qualified name of those declarations still contain the module's name.


Within code, the fully qualified name of a declaration can almost always be used to refer to a type.

 Namespace A   Class B   End Class End Namespace Namespace C   Module Test     Sub Main()       Dim x As A.B     End Sub   End Module End Namespace 

The previous sentence was qualified with "almost always" because it is possible to "hide" the outermost namespace that a declaration is contained in, making it impossible to refer to the type by its fully qualified name.

 Namespace A   Class B   End Class End Namespace Namespace C   Class A   End Class   Module Test     Sub Main()       Dim x As A.B     End Sub   End Module End Namespace 

In this last example, when looking up the name A in the context of the subroutine Test , the compiler will first find the declaration for class C.A and assume that is what A is referring to. Since there is no member of C.A named B , the compiler will give an error.

 <  Day Day Up  >  


The Visual Basic .NET Programming Language
The Visual Basic .NET Programming Language
ISBN: 0321169514
EAN: 2147483647
Year: 2004
Pages: 173
Authors: Paul Vick

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net