Creating Your Own Namespaces


Every assembly created in .NET is part of some root namespace. By default, this logic actually mirrors COM, in that assemblies are assigned a namespace that matches the project name. However, unlike COM, in .NET it is possible to change this default behavior. In this way, just as Microsoft has packaged the system-level and CLR classes using well-defined names, it is possible for you to create your own namespaces. Of course, it’s also possible to create projects that match existing namespaces and extend those namespaces, but that is very poor programming practice.

Creating an assembly in a custom namespace can be done at one of two levels. However, unless you want the same name for each assembly that will be used in a large namespace, you would normally reset the root namespace for the assembly. This is done through the assembly’s project pages, reached by right-clicking the solution name in the Solution Explorer window and working off the first tab (Application) within the Properties page that opens in the Document window, as shown in Figure 8-8.

image from book
Figure 8-8

The next step is optional, but, depending on whether you want to create a class at the top level or at a child level, you can add a Namespace command to your code. There is a trick to being able to create toplevel namespaces or multiple namespaces within the modules that make up an assembly. Instead of replacing the default namespace with another name, you can delete the default namespace and define the namespaces only in the modules, using the Namespace command.

The Namespace command is accompanied by an End Namespace command. This End Namespace command must be placed after the End Class tag for any classes that will be part of the namespace. The following code demonstrates the structure used to create a MyMetaNamespace namespace, which contains a single class:

  Namespace MyMetaNamespace     Class MyClass1        ' Code     End Class End Namespace 

You can then utilize the MyClass1 object simply by referencing its namespace, MyMetaNamespace. MyClass1. It is also possible to have multiple namespaces in a single file, as shown here:

  Namespace MyMetaNamespace1     Class MyClass1        ' Code     End Class  End Namespace Namespace MyMetaNamespace2    Class MyClass2       ' Code     End Class  End Namespace 

Using this kind of structure, if you want to utilize MyClass1, then you get at it through the namespace MyMetaNamespace.MyClass1. This does not give you access to MyMetaNamespace2 and the objects that it offers; instead, you have to make a separate reference to MyMetaNamespace2.MyClass2.

The Namespace command can also be nested. Using nested Namespace commands is how child namespaces are defined. The same rules apply - each Namespace must be paired with an End Namespace and must fully encompass all of the classes that are part of that namespace. In the following example, the MyMetaNamespace has a child namespace called MyMetaNamespace.MyChildNamespace:

  Namespace MyMetaNamespace     Class MyClass1        ' Code     End Class     Namespace MyChildNamespace        Class MyClass2           ' Code        End Class     End Namespace End Namespace 

This is another point to be aware of when you make references to other namespaces within your own custom namespaces. Consider the following example:

  Imports System Imports System.Data Imports System.Data.SqlClient Namespace MyMetaNamespace1     Class MyClass1        ' Code     End Class End Namespace Namespace MyMetaNamespace2    Imports System.IO        Class MyClass2       ' Code    End Class End Namespace 

In this example, a number of different namespaces are referenced in the file. The three namespaces referenced at the top of the code listing - the System, System.Data, and System.Data.SqlClient namespace references - are available to every namespace developed in the file. This is because these three references are sitting outside of any particular namespace declarations. However, the same is not true for the System.IO namespace reference. Because this reference is made within the MyMetaNamespace2 namespace, it is unavailable to any other namespace in the file.

Important 

When you create your own namespaces, Microsoft recommends that you use a convention of CompanyName.TechnologyName - for example, Wrox.Books. This helps to ensure that all libraries are organized in a consistent way.

Sometimes when you are working with custom namespaces, you might find that you have locked yourself out of accessing a particular branch of a namespace, purely due to naming conflicts. For this reason, Visual Basic has introduced the new Global keyword in this latest version, which can be used as the outermost root class available in the .NET Framework class library. Figure 8-9 shows a diagram of how the class structure looks with the new Global keyword.

image from book
Figure 8-9

This means that you can make specifications such as

  Global.System.String 

or

  Global.Wrox.System.Titles 




Professional VB 2005 with. NET 3. 0
Professional VB 2005 with .NET 3.0 (Programmer to Programmer)
ISBN: 0470124709
EAN: 2147483647
Year: 2004
Pages: 267

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