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
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
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:
- Namespace <Namespace_name>
-
- // Classes and/or structs and/or enums etc.
-
- 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.
- Imports System
- Namespace Birds
- Class Parrot
- Public Shared Function fly()
- Console.WriteLine("Parrot can fly")
- End Function
- Public Shared Function color()
- 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()
- End Function
- Sub main()
- Console.Clear()
- Birds.Parrot.fly()
- ConsoleApplication5.Birds.Parrot.color()
- myfunction()
- End Sub
- End Module
Output
- Namespace MyNamespace
- Public Class Class1
- Public Shared Function disp()
- Console.Write("hello" & vbCrLf)
- End Function
- End Class
- End Namespace
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
- Imports System
- Namespace outer
- Public Class nameout
- Public Shared Function disp()
- Console.WriteLine("hi this is outer name space")
- End Function
- End Class
-
- Namespace inner
- Public Class nameinn
- Public Shared Function disp()
- Console.WriteLine("hi this is inner namespace")
- End Function
- End Class
- End Namespace
-
- End Namespace
-
- Module module1
- Sub main()
- Console.Clear()
- outer.nameout.disp()
- outer.inner.nameinn.disp()
- End Sub
- End Module
Output
Note: You can not have two classes with the same name in the same scope. In other words, class overloading is not allowed.
Example
- Namespace MyNamespace
- Public Class one
- Public Shared Function disp()
- Console.Write("hello" & vbCrLf)
- End Function
- End Class
-
- Public Class one
- Public Shared Function disp1()
- Console.Write("hi")
- End Function
- End Class
- End Namespace
You can avoid this by putting classes with the same name in a different scope.
Example
- Namespace outerscope
- Public Class one
- Public Shared Function disp()
- Console.Write("hello class with outerscope" & vbCrLf)
- End Function
- End Class
- Namespace innerscope
- Public Class one
- 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
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
- Imports ally = ConsoleApplication6.outerscope.innerscope
- Namespace outerscope
- Public Class one
- Public Shared Function disp()
- Console.Write("hello class with outer scope" & vbCrLf)
- End Function
- End Class
- Namespace innerscope
- Public Class one
- 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()
- ally.one.disp1()
- End Sub
- End Module
Output