Developers utilize namespaces when they want to place their classes within specific .NET namespaces. Using a namespace statement with the default namespace that's identified in a project's properties allows you to fulfill this requirement.
Namespaces
By default, every VB.NET application resides within the default namespace, which is often referred to as an absolute namespace position of the application. Visual Studio .NET sets this to the top-level namespace and automatically assigns it the name that matches the name of your project.
Creating a new WindowsApplication1 (the default name assigned to a new project you start in Visual Studio .NET for Windows applications) will set the namespace to WindowsApplication1. If you want to modify the project's namespace, you need to open the Project Properties window and modify the Root namespace field in the Application tab. If you set the namespace to an existing namespace, such as System.Windows.Forms, you have to make sure that the class names don't conflict with classes already defined in that namespace.
Naming conventions
When generating a full .NET application (exe), the name you choose for the namespace is not extremely important since that namespace will only exist within your program's view and its lifetime. Two applications using the WindowsApplication1 namespace will not conflict with each other. If you generate a .NET library (dll) that will be distributed to people outside of your group or organization, make sure you select a namespace that will not conflict with other namespaces.
Microsoft's general recommendation about naming conventions for namespaces is to use a combination of your company name with the product name.
Positioning
Beyond the absolute namespace position, you can put your classes in the relative namespace position within the larger default absolute namespace. When you add a class to your project, it appears in the absolute namespace position.
Class Class1
End Class
If your project uses WindowsApplication1 as its absolute namespace, this class appears as Windows.Application1.Class1.
In relative positioning, you can insert a new namespace between the absolute position and the class:
Namespace GeneralApplications
Class Class1
End Class
End Namespace
In this case, Class1 is fully referenced as WindowsApplication1.GeneralApplications.Class1.
Keyword
The namespace keyword may include multiple namespace components separated by periods. Keep in mind that namespaces may also be nested.
Additional TechRepublic resources about namespaces
- What's in a namespace?
- Resolve host names with .NET
- Developing components: Namespacing and code organization
Miss a tip?
Check out the Visual Basic archive, and catch up on the most recent editions of Irina Medvinskaya's column.
Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!



