Reader Level:

Understanding and using Namespaces in VB.NET

Posted by G Gnana Arun Ganesh Articles | Visual Basic .NET November 10, 2012
In this article, you will learn about namespaces in VB.NET. Here you will learn how to create and use namespaces.
  • 0
  • 0
  • 14001


This article bring in you to VB.NET Namespaces. In this article, my objectives are as follows:

  • To give you the picture of what Namespace is?
  • Learn how to implement the "using" directive?
  • Learn to use "alias" directives.
  • Understand what are namespace members?

Need For Namespaces.

Namespaces allow you to create a system to organize your code. A good way to organize your namespaces is via a hierarchical system. You put the more general names at the top of the hierarchy and get more specific as you go down. This hierarchical system can be represented by nested namespaces.By placing code in different sub-namespaces, you can keep your code organized.

The example.


Public Class Hello
Public Function GetMessage() As String
"Hello, world"
End Function 'GetMessage
End Class 'Hello
End Namespace 'arun.VB.Namespaces

Namespaces are hierarchical, and the name arun.VB.Namespaces is actually shorthand for defining a namespace named arun that contains a namespace named VB that itself contains a namespace named Namespaces, as in:


Namespace CSharp
Namespace Namespaces
End Namespace 'Namespaces '
End Namespace 'CSharp '
End Namespace 'arun

The using keyword has two uses:

  • Create an alias for a namespace (a using alias).
  • Permit the use of types in a namespace, such that, you do not have to qualify the use of a type in that namespace (a using directive).

Java programmers should note that, we could use namespace first, followed by using or vice versa. The only purpose of the using command in this context is to save you typing and make your code simpler. It does not, for example, cause any other code or libraries to be added to your project. If your code uses base classes, you need to ensure separately that the compiler knows which assemblies to look in for the classes (/r switch in the compiler).

Implement The "using" Directive.

Next, we'll write a console application that uses the Hello class. We could just use the fully qualified name for the class-arun.CSharp.Namespaces.Hello - but this name is quite long and unwieldy. An easier way is to use a "using" directive, which makes it possible to use all of the types in a namespace without qualification. If you would like to call methods without typing their fully qualified name, you can implement the "using" directive.


Imports arun.CSharp.Namespaces
Class Hello
Shared Sub Main()
Dim mAs New Hello
End Sub 'Main
End Class 'Hello

"Imports System", is the same "Imports " directive you have seen in every program in this article. It allows you to type the method names of members of the "System" namespace without typing the word "System" every time. In class Hello(), "Console" is a class member of the "System" namespace with the method "WriteLine". It's fully qualified name is "System.Console.WriteLine(...)".

Note that the two occurrences of Hello are shorthand for arun.CSharp.Namespaces.Hello. VB.NET also enables the definition and use of aliases. Such aliases can be useful in situation in which name collisions occur between two libraries, or when a small number of types from a much larger namespace are being used.

Imports alias directives.

A Imports-alias-directive introduces an identifier that serves as an alias for a namespace or type within the immediately enclosing compilation unit or namespace body.

Imports identifier = namespace-or-type-name;

Within member declarations in a compilation unit or namespace body that contains a using-alias-directive, the identifier introduced by the Imports-alias-directive can be used to reference the given namespace or type.

For example:

Namespace N1.N2
Class A
End Class 'A
End Namespace 'N1.N2
Imports A = N1.N2.A
Namespace N3
Class B
Inherits A
End Namespace 'N3

Here, within member declarations in the N3 namespace, A is an alias for N1.N2.A, and thus class N3.B derives from class N1.N2.A. The same effect can be obtained by creating an alias R for N1.N2 and then referencing R.A:


R = N1.N2
Namespace N3
Class B
Inherits R.A
End Class 'B
End Namespace 'N3

The identifier of a using-alias-directive must be unique within the declaration space of the compilation unit or namespace that immediately contains the using-alias-directive.

Using namespace directives.

A using-namespace-directive imports the types contained in a namespace into the immediately enclosing compilation unit or namespace body, enabling the identifier of each type to be used without qualification.

using namespace-name;

Within member declarations in compilation unit or namespace body that contains a using-namespace-directive, the types contained in the given namespace can be referenced directly.

For example:


Class A
End Class 'A
End Namespace 'N1.N2
Namespace N3
Class B
Inherits A
End Namespace 'N3

Here, within member declarations in the N3 namespace, the type members of N1.N2 are directly available, and thus class N3.B derives from class N1.N2.A. Like a using-alias-directive, a using-namespace-directive does not contribute any new members to the underlying declaration space of the compilation unit or namespace, but rather affects only the compilation unit or namespace body in which it appears.

The namespace-name referenced by a using-namespace-directive is resolved in the same way as the namespace-or-type-name referenced by a using-alias-directive. Thus, using-namespace-directives in the same compilation unit or namespace body do not affect each other and can be written in any order.


VB.NET programs are organized using namespaces. Using directives are provided to facilitate the use of namespaces. From this article we can understand the need and usage of Namespaces in classes.The Namespaces can hold other types also as follows:

Classes,Structures,Interfaces,Enumerations,Delegates.Namespaces are used both as an "internal" organization system for a program, and as an "external" organization system - a way of presenting program elements that are exposed to other programs.



Trending up