Working with VB Namespaces

Code can become disorganized in a blink of an eye! One moment you think you remember where your classes are nested into, the next you are lost among a plethora of coding instructions and files. VB Namespaces to the rescue!

VB Namespaces

VB Namespaces organize all the objects defined in an assembly. Assemblies can contain multiple Namespaces that contain several classes, which in turn can contain other Namespaces. Within a VB Namespace, you can create items such as classes, modules, structures, interfaces, delegates, enumerations, and even other Namespaces. VB Namespaces prevent ambiguity and makes references to methods easier, especially when using objects that contain groups of objects such as class libraries.

VB Namespaces address a problem commonly called Namespace pollution. This happens when a developer of a class library is hampered by the use of similar names in another library. These conflicts are called name collisions.

Fully Qualified Names

Fully qualified names are object references that are prefixed with the name of the VB Namespace in which the object is defined. Fully qualified names (FQN) can prevent naming conflicts because fully qualified names make it possible for the compiler to determine which object from which VB Namespace is being used. The following code shows how to use the fully qualified name for an object from another project’s VB Namespace:

Dim ExternalTextBox As New Project2.Form1.TextBox1

Sometimes, however, the object references themselves can get long and cumbersome, so much so that you have to use the Imports statement to define an abbreviated name you can use in place of a fully qualified name. The following code creates an alias for a fully qualified name, and uses this alias to define the object:

Imports ExternalTextBox = Project2.Form1.TextBox1

If you were to use an Imports statement without an alias for a fully qualified name, you can use all the names in that VB Namespace without any qualification, provided they are unique to the project.

The Global Keyword

You can use the Global keyword to access other root-level Namespaces. If you have defined a nested hierarchy of VB Namespaces, code inside that hierarchy might accidentally get blocked from being accessed. To prevent code from accidentally being blocked, you should use the Global keyword to start the qualification chain at the outermost level of the .NET Framework class library again.

Imported Namespaces

Imported Namespaces are managed through the References page of the Project Designer. The imports you specify in this dialog box are passed directly to the compiler.

To add an imported Namespace:

  1. In Solution Explorer, double-click the My Project node for the project.
  2. In the Project Designer, click the References tab.
  3. In the Imported Namespaces list, select the check box for the Namespace that you want to add.

To remove an imported Namespace:

  1. In Solution Explorer, double-click the My Project node for the project.
  2. In the Project Designer, click the References tab.
  3. In the Imported Namespaces list, clear the check box for the Namespace that you wish to remove.

User Imports

User imports allow you to import specific classes within VB Namespaces rather than the entire VB Namespace. For example: Your application might have an import for the Systems.Diagnostics Namespace, but the only class within that Namespace that you need is the Debug class. So, you can then define System.Diagnostics.Debug as a user import instead, and then remove the import for System.Diagnostics.

To add a user import:

  1. In Solution Explorer, double-click the My Project node for the project.
  2. In the Project Designer, click the References tab.
  3. In the text box below the Imported Namespaces list, enter the full name for the Namespace you wish to import, including the root Namespace.
  4. Click the Add user import button to add the Namespace to the Imported Namespaces list.

To update a user import:

  1. In Solution Explorer, double-click the My Project node for the project.
  2. In the Project Designer, click the References tab.
  3. In the Imported Namespaces list, select the Namespace you want to change.
  4. In the text box below the Imported Namespaces list, enter the name for the new Namespace.
  5. Click the Update user import button to update the Namespace in the Imported Namespaces list.

The .NET Framework Class Library

The .NET Framework class library is a library of classes and interfaces that provides access to system functionality. It is the foundation on which .NET Framework applications, components, and controls are built. To find a complete list of all available Namespaces in the .NET Framework, have a look on MSDN.

Conclusion

Namespaces are there for one simple reason: code organization. The better we can structure our code libraries, the better we can maintain them.

Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read