A namespace designates a collection of programming elements, organized and classed for grouping operations and easy access. At the namespace level, the programming elements include:
- Classes of objects [...];
- Structures [...];
- Modules [...];
- Interfaces [...];
- Delegates [...];
- Enumeration [...];
- Other namespaces.
Namespaces can be organized, aggregated into an assembly [...]. An assembly can contain multiple namespaces, which can contain other namespaces.
By default, the executable file created with Visual Studio environment contains a namespace with a name identical to that of project. More assemblies can use the same namespace.
Explicit definition of a namespace is done in VB.NET with specifications Namespace and End Namespace. It considers an application of type Console Application. It defines the namespace ProiectNou that contains:
- A namespace called Formular1;
- A class called GrupOp which contains two methods: Adun and Imp.
The namespace Formular1 contains the class GrupOp with an event two methods: Dif and Prod.
It notes the presence of two classes of objects with the same name GrupOp, but included in different namespaces: ProiectNou, respectively Formular1.
The namespace ProiectNou is defined on the same level as the procedure Main is defined.
Namespace ProiectNou Namespace Formular1 Class GrupOp Public Event CalculOp(ByVal x As Integer, ByVal y As Integer) Public Function Dif(ByVal a As Integer, ByVal b As Integer)_ As Integer Return a - b End Function Public Function Prod(ByVal a As Integer, ByVal b As Integer)_ As Long Return a * b End Function End Class End Namespace Class GrupOp Public Function Adun(ByVal a As Integer, ByVal b As Integer)_ As Integer Return a + b End Function Public Function Imp(ByVal a As Integer, ByVal b As Integer)_ As Double Return a / b End Function End Class End Namespace Module Module1 Sub Main() Dim calc As NameSpaceApp.ProiectNou.GrupOp calc = New NameSpaceApp.ProiectNou.GrupOp MsgBox("4+6=" & calc.Adun(4, 6)) Dim calcNou As NameSpaceApp.ProiectNou.Formular1.GrupOp calcNou = New NameSpaceApp.ProiectNou.Formular1.GrupOp MsgBox("4*6=" & calcNou.Prod(4, 6)) End Sub End Module
In the procedure Main, it is noted that to define a variable of type GrupOp it has to specify the complete path to the class GrupOp by expressions NameSpaceApp.ProiectNou.GrupOp and NameSpaceApp.ProiectNou.Formular1.GrupOp. Otherwise, the application can not be compiled because it does not “know” where to extract the definition of the class GrupOp from.
NameSpaceApp is namespace at project level created by Visual Studio environment.
Referring of a programming item in VB.NET source code is done by specification Imports.
Referring the programming elements is done starting with the namespace with the highest level of aggregation and it provides the paths of subcollections included in namespace by using operator . .
Thus, it accesses the programming element GrupOp included in Formular1, which it is included in the namespace ProiectNou. The namespace ProiectNou is defined in the namepsace at the application level, called with the same name like the project NameSpaceApp.
The complete referring of the list of items in the programming elements included in namespaces, avoids conflict by their given identical names.
For ease of operation with elements of namespace, it can assign an alias to refer a program element included in a namespace. Alias allows direct reference to this element.
Imports GrupOperatii = NameSpaceApp.ProiectNou.Formular
If the first example, namespace was defined in the same file with VB.NET source code module that has been used elements defined in ProiectNou. If ProiectNou namespace is defined in another source file included in the project NameSpaceApp, then before developing the module Module1 it should be included specifications Imports, as it follows:
Imports G1 = NameSpaceApp.ProiectNou Imports G2 = NameSpaceApp.ProiectNou.Formular1 Module Module1 Sub Main() Dim calc As G1.GrupOp calc = New G1.GrupOp MsgBox("4+6=" & calc.Adun(4, 6)) Dim calcNou As G2.GrupOp calcNou = New G2.GrupOp MsgBox("4*6=" & calcNou.Prod(4, 6)) End Sub End Module
The namespace ProiectNou has the same definition, but included in another source file in the application NameSpaceApp. In a namespace, the following programming elements cannot be defined: properties [...], procedures [...], variables [...] and events [...]. These elements are defined in source code containers: modules, structures and classes.