Working with VB Namespaces

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

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.



About the Author

Hannes DuPreez

Hannes du Preez has been a Microsoft MVP for Visual Basic from 2008 to 2017. He loves technology and loves Visual Basic. He loves writing articles and proving that Visual Basic is more powerful than what most believe. You are most welcome to reach him at: ojdupreez1978@gmail.com

Related Articles

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date