Export (0) Print
Expand All

Namespaces

Namespaces organize the objects defined in an assembly. Assemblies can contain multiple namespaces, which can in turn contain other namespaces. Namespaces prevent ambiguity and simplify references when using large groups of objects such as class libraries.

For example, Visual Studio .NET defines the ListBox class in the System.Windows.Forms namespace. The following code fragment shows how to declare a variable using the fully qualified name for this class:

Dim LBox As System.Windows.Forms.ListBox

Visual Studio .NET namespaces address a problem sometimes known as namespace pollution, in which the developer of a class library is hampered by the use of similar names in another library. These conflicts with existing components are sometimes called name collisions.

For example, if you create a new class named ListBox, you can use it inside your project without qualification. However, if you want to use the Visual Studio .NET ListBox class in the same project, you must use a fully qualified reference to make the reference unique. If the reference is not unique, Visual Basic .NET produces an error stating that the name is ambiguous. The following code example demonstrates how to declare these objects:

'   Define a new object based on your ListBox class.
Dim LBC as New ListBox 
'   Define a new Windows.Forms ListBox control.
Dim MyLB as New System.Windows.Forms.ListBox 

The following illustration shows two namespace hierarchies, both containing an object named ListBox.

By default, every executable file you create with Visual Basic .NET contains a namespace with the same name as your project. For example, if you define an object within a project named ListBoxProject, the executable file, ListBoxProject.exe, contains a namespace called ListBoxProject.

Multiple assemblies can use the same namespace. Visual Basic .NET treats them as a single set of names. For example, you can define classes for a namespace called SomeNameSpace in an assembly named Assemb1, and define additional classes for the same namespace from an assembly named Assemb2.

Fully Qualified Names

Fully qualified names are object references that are prefixed with the name of the namespace where the object is defined. You can use objects defined in other projects if you create a reference to the class (by choosing Add Reference from the Project menu) and then use the fully qualified name for the object in your code. The following code fragment shows how to use the fully qualified name for an object from another project's namespace:

Dim LBC As New ListBoxProject.Form1.ListBox()

Fully qualified names prevent naming conflicts because the compiler can always determine which object is being used. However, the names themselves can get long and cumbersome. To get around this, you can use the Imports statement to define an alias — an abbreviated name you can use in place of a fully qualified name. For example, the following code example creates aliases for two fully qualified names, and uses these aliases to define two objects:

Imports LBControl = System.Windows.Forms.ListBox

Imports MyListBox = ListBoxProject.Form1.ListBox

Dim LBC As LBControl

Dim MyLB As MyListBox

If you use the Imports statement without an alias, you can use all the names in that namespace without qualification provided they are unique to the project. If your project contains Imports statements for namespaces that contain items with the same name, you must fully qualify that name when you use it. Suppose, for example, your project contained the following two Imports statements:

Imports MyProj1 ' This namespace contains a class called Class1.
Imports MyProj2 ' This namespace also contains a class called Class1.

If you attempt to use Class1 without fully qualifying it, Visual Basic .NET produces an error stating that the name Class1 is ambiguous.

Namespace Level Statements

Within a namespace, you can define items such as modules, interfaces, classes, delegates, enumerations, structures, and other namespaces. You cannot define items such as properties, procedures, variables and events at the namespace level, these items must be declared within containers such as modules, structures, or classes.

See Also

Assemblies (Visual Basic .NET) | Creating and Using Assemblies | References and the Imports Statement | Imports Statement | Language Changes in Visual Basic | ListBox Class | System.Windows.Forms Namespace

Show:
© 2014 Microsoft