ARTICLE

Namespaces in VB.Net

Posted by Sanjeev Kumar Articles | Active Directory in VB.NET March 09, 2011
This article provides a brief overview about namespaces in VB.Net. I hope this article will be helpful to all.
Download Files:
 
Reader Level:

Namespace in .NET:-    This article will help you to understand brief overview about Namespaces in .NET.

Introduction:-

Software projects consist of several pieces of code such as classes, declaration, 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 'Namespace' or we can say that the group of code having 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. Namespace sometime 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 different or same meaning. Every project in c# or VB.NET starts with a Namespace, by default 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. 'Imports' keyword is used to add a reference of a name space manually.

Example:-

  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:-

  Imports System.Collections

Namespace 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 hierarchal structure.
Namespaces allow us to organize Classes so that they can be easily accessed in other applications. Namespace 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.

Declaring a Namespace:-

We can define a Namespace by using 'Namespace' keyword. The syntax for declaring a Namespace is :-

  Namespace <Namespace_name>

    // Classes and/or structs and/or enums etc.

End Namespace  

Example:-

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

Note:- All the classes in .Net Framework  belongs to 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 member of a Namespace by using dot(.) operator also known as period operator. The members of a Namespace are the variable, procedure, 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 dot or period operator followed by the desired member of the namespace.

 Example:-

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

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

Imports
System
Namespace Birds    'user defined namespace Bird
    Class Parrot  'Parrot is a class in the namespace Animals
        Public Shared Function fly()  'Fly is a function in this Class
            Console.WriteLine("Parrot can fly")
        End Function
        Public Shared Function color() ' color is another function in parrot class
            Console.WriteLine("normally Parrots are green")
        End Function
        Public Shared Function type()
            Console.WriteLine("Different type of parrot are found around the world")
        End Function
    End Class
End
Namespace

Module
Module1
    Public Function myfunction()
        Dim P As Birds.Parrot
        P = New Birds.Parrot()
        P.type()     'accessing member of the namespace bird            
    End Function
    Sub main()
        Console.Clear()
        Birds.Parrot.fly()       'accessing member of the namespace
        ConsoleApplication5.Birds.Parrot.color() 'another way to access member of the
                                                              namespace                        
        myfunction()
    End Sub
End
Module

Output:-

example1.gif

Nesting a Namespace:-

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

Example:-

Imports
System
Namespace outer   'declare an outer namespace
    Public Class nameout
        Public Shared Function disp()  'create a function inside a outer namespace
            Console.WriteLine("hi this is outer name space")
        End Function
    End Class

    Namespace inner  'decalre an inner namespace
        Public Class nameinn
            Public Shared Function disp()  'create a function inside the inner namespace
                Console.WriteLine("hi this is inner namespace")
            End Function
        End Class
    End Namespace

End Namespace

Module
module1
    Sub main()
        Console.Clear()
        outer.nameout.disp()        'accesing function of the outer namespace
        outer.inner.nameinn.disp()  'accessing fucntion of the inner namespace
    End Sub
End
Module

Output:- 

example2.gif

Note:- You can not have two classes with the same name in the same scope. Means class overloading is not allowed.

e.g.:-

Namespace MyNamespace         
    Public Class one   'sample class with in a namespace
        Public Shared Function disp()  'function declared within the class
            Console.Write("hello" & vbCrLf)
        End Function
    End Class

    Public Class one  'this is not allowed
        Public Shared Function disp1()
            Console.Write("hi")
        End Function
    End Class
End
Namespace
You can avoid this by putting  classes with same name in different scope.

e.g.:-

Namespace
outerscope          'sample class with in a namespace
    Public Class one
        Public Shared Function disp()  'function declared within the class
            Console.Write("hello class with outerscope" & vbCrLf)
        End Function
    End Class
    Namespace innerscope
        Public Class one    'same class with different scope
            Public Shared Function disp1()
                Console.Write("hi this class is wihtin innerscope of outscope namespace " & vbCrLf)
            End Function
        End Class
    End Namespace
End
Namespace

Module
module1
    Sub main()
        Console.Clear()
        outerscope.one.disp()
        outerscope.innerscope.one.disp1()
    End Sub
End
Module

Output:-
example4.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 'imports' keyword. Aliasing is useful when we are working with a large project.

Example:-

Imports ally = ConsoleApplication6.outerscope.innerscope 'creating an alias name for inner scope
Namespace outerscope          'sample class with in a namespace
    Public Class one
        Public Shared Function disp()  'function declared within the class
            Console.Write("hello class with outer scope" & vbCrLf)
        End Function
    End Class
    Namespace innerscope
        Public Class one    'same class with different scope
            Public Shared Function disp1()
                Console.Write("hi accessing the member with the alias name" & vbCrLf)
            End Function
        End Class
    End Namespace
End
Namespace

Module
module1
    Sub main()
        Console.Clear()
        outerscope.one.disp()  'accessing member of the outerscope namespace without alias name
        ally.one.disp1()      'accessing member of the innerscope  by alias name
    End Sub
End
Module

Output:-
example3.gif



  

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor