What is VB.NET Namespace

Software projects consist of several pieces of code such as classes, declarations, procedures and functions etc., known as the component or identifiers of the software project. In large projects the number of these components can be very large. These components can be grouped into smaller subcategories. This logical grouping construct is known as a "Namespace" or we can say that the group of code having a specific name is a "Namespace". In a Namespace the groups of components are somehow related to each other. Namespaces are similar in concept to a folder in a computer file system. Like folders, namespaces enable classes to have a unique name or we can say that it is a logical naming scheme for grouping related types. A Namespace is sometimes also called a name scope. An identifier defined in a Namespace belongs to that Namespace and the same identifier can be independently defined in multiple Namespaces with a different or the same meaning. Every project in C# or VB.NET starts with a Namespace, by default the same name as the name of the project.

Why we need it

We must add a reference of the Namespace object before using that object in a project. Several references are automatically added in the project by default. The VB.Net "Imports" keyword is used to add a reference of a namespace manually.

Example

  1. Imports System
Note: Imports allow access to classes in the referenced Namespace only not in its internal or child Namespaces. If we want to access internal Namespace we might need to write:
  1. Imports System.Collections
Namespaces are basically used to avoid naming collisions, when we have multiple classes with the same name, and it is also helpful for organizing classes libraries in a hierarchal structure. Namespaces allow us to organize Classes so that they can be easily accessed in other applications. Namespaces also enable reusability.

A class in .Net Framework cannot belong to multiple Namespaces. One class should belong to only one Namespace. VB.NET does not allow two classes with the same name to be used in a program. 

We can define a Namespace using the "Namespace" keyword. The syntax for declaring a Namespace is:

  1. Namespace <Namespace_name>  
  2.   
  3.     // Classes and/or structs and/or enums etc.  
  4.   
  5. End Namespace  
Example

Note: All the classes in the .Net Framework belongs to the System Namespace. The "system" Namespace has built-in VB functionality and all other Namespaces are based on this "system" Namespace.

Accessing Members of a Namespace

We can access a member of a Namespace by using a dot(.) operator, also known as the period operator. The members of a Namespace are the variables, procedures and classes that are defined within a Namespace. To access the member of a namespace in a desired location type the name of the namespace followed by the dot or period operator followed by the desired member of the namespace.

Example

MyNamespace.Class1.disp()   'Accessing elements of the MyNamspace

we can access a member of a namespace in various ways. The following program shows accessing the element of a namespace in various ways.

  1. Imports System  
  2. Namespace Birds    'user defined namespace Bird   
  3.     Class Parrot  'Parrot is a class in the namespace Animals   
  4.         Public Shared Function fly()  'Fly is a function in this Class  
  5.             Console.WriteLine("Parrot can fly")  
  6.         End Function  
  7.         Public Shared Function color() ' color is another function in parrot class  
  8.             Console.WriteLine("normally Parrots are green")  
  9.         End Function  
  10.         Public Shared Function type()  
  11.             Console.WriteLine("Different type of parrot are found around the world")  
  12.         End Function  
  13.     End Class  
  14. End Namespace  
  15.   
  16. Module Module1  
  17.     Public Function myfunction()  
  18.         Dim P As Birds.Parrot  
  19.         P = New Birds.Parrot()  
  20.         P.type()     'accessing member of the namespace bird              
  21.     End Function  
  22.     Sub main()  
  23.         Console.Clear()  
  24.         Birds.Parrot.fly()       'accessing member of the namespace   
  25.         ConsoleApplication5.Birds.Parrot.color() 'another way to access member of the namespace                          
  26.         myfunction()  
  27.     End Sub  
  28. End Module  

Output 

  1. Namespace MyNamespace                  'class with in a namespace  
  2.     Public Class Class1  
  3.         Public Shared Function disp()       'function declared within the class   
  4.             Console.Write("hello" & vbCrLf)  
  5.         End Function  
  6.     End Class  
  7.  End Namespace  

Access-element-from-namespace-in-vbdotnet.gif

Nesting a Namespace

Nesting a Namespace means create a namespace inside a namespace. A good way to organize namespaces is to put them in a hierarchal order, i.e. general name at the top of the hierarchy and put specific names at the lower level.

Example

  1. Imports System  
  2. Namespace outer   'declare an outer namespace  
  3.     Public Class nameout  
  4.         Public Shared Function disp()  'create a function inside a outer namespace  
  5.             Console.WriteLine("hi this is outer name space")  
  6.         End Function  
  7.     End Class  
  8.   
  9.     Namespace inner  'decalre an inner namespace  
  10.         Public Class nameinn  
  11.             Public Shared Function disp()  'create a function inside the inner namespace  
  12.                 Console.WriteLine("hi this is inner namespace")  
  13.             End Function  
  14.         End Class  
  15.     End Namespace  
  16.   
  17. End Namespace  
  18.   
  19. Module module1  
  20.     Sub main()  
  21.         Console.Clear()  
  22.         outer.nameout.disp()        'accesing function of the outer namespace  
  23.         outer.inner.nameinn.disp()  'accessing fucntion of the inner namespace  
  24.     End Sub  
  25. End Module

Output

nested-namspace-in-vbdotnet.gif

Note: You can not have two classes with the same name in the same scope. In other words, class overloading is not allowed.

Example

  1. Namespace MyNamespace            
  2.     Public Class one   'sample class with in a namespace  
  3.         Public Shared Function disp()  'function declared within the class   
  4.             Console.Write("hello" & vbCrLf)  
  5.         End Function  
  6.     End Class  
  7.   
  8.     Public Class one  'this is not allowed   
  9.         Public Shared Function disp1()  
  10.             Console.Write("hi")  
  11.         End Function  
  12.     End Class  
  13. End Namespace  
You can avoid this by putting classes with the same name in a different scope.

Example

  1. Namespace outerscope          'sample class with in a namespace  
  2.     Public Class one  
  3.         Public Shared Function disp()  'function declared within the class   
  4.             Console.Write("hello class with outerscope" & vbCrLf)  
  5.         End Function  
  6.     End Class  
  7.     Namespace innerscope  
  8.         Public Class one    'same class with different scope  
  9.             Public Shared Function disp1()  
  10.                 Console.Write("hi this class is wihtin innerscope of outscope namespace " & vbCrLf)  
  11.             End Function  
  12.         End Class  
  13.     End Namespace  
  14. End Namespace  
  15.   
  16. Module module1  
  17.     Sub main()  
  18.         Console.Clear()  
  19.         outerscope.one.disp()  
  20.         outerscope.innerscope.one.disp1()  
  21.     End Sub  
  22. End Module  
Output

Simple-namespace-in-class-vbdotnet.gif

Aliases of the Namespaces

Aliases are created when we have nested Namespaces. It is easy to access the members of the namespaces by the alias. An alias is a shortcut for a  nested namespace with a shorter label. An alias of a namespace is created with the help of the "imports" keyword. Aliasing is useful when we are working with a large project.

Example

  1. Imports ally = ConsoleApplication6.outerscope.innerscope 'creating an alias name for inner scope  
  2. Namespace outerscope          'sample class with in a namespace  
  3.     Public Class one  
  4.         Public Shared Function disp()  'function declared within the class   
  5.             Console.Write("hello class with outer scope" & vbCrLf)  
  6.         End Function  
  7.     End Class  
  8.     Namespace innerscope  
  9.         Public Class one    'same class with different scope  
  10.             Public Shared Function disp1()  
  11.                 Console.Write("hi accessing the member with the alias name" & vbCrLf)  
  12.             End Function  
  13.         End Class  
  14.     End Namespace  
  15. End Namespace  
  16.   
  17. Module module1  
  18.     Sub main()  
  19.         Console.Clear()  
  20.         outerscope.one.disp()  'accessing member of the outerscope namespace without alias name  
  21.         ally.one.disp1()      'accessing member of the innerscope  by alias name  
  22.     End Sub  
  23. End Module  
Output

aliases-of-the-Namespaces-in-vbdotnet.gif