Namespaces

Specific program contexts require the space name to be specified. The full name of a namespace is generated using one or more identifiers separated by points.

A namespace definition includes the Namespace keyword followed by the namespace identifier and its body and appended by a sequence of End Namespace <ident> keywords. A namespace can be defined at the upper level within a unit (form) or inside another namespace. When a namespace is defined at the upper level within a compilation unit, this namespace becomes a member of the global namespace (Global Scope). When a namespace is defined within another namespace, the nested namespace becomes a member of the enclosing one. In both cases the namespace name must be unique within the enclosing namespace. Namespaces are always used in the Public access mode and their definitions cannot contain access modifiers.

Namespaces are extended if two namespace definitions with the same fully qualified names are added to the same definition space.

All members described within the namespace with one qualified name must have unique names. This rule should also be observed for the members described in the Global Scope global namespace. The Global Scope namespace is global for all compilation units within one assembly.

Example

Namespaces description:

Namespace A
    
Class MyClass: Object
        
Public Sub Run;
        
Begin
            B.Proc1;
            B.Proc2;
            Proc4;
        
End Sub Run;

        
Sub Proc4;
        
Begin
            Debug.WriteLine(
"The Proc4 procedure is executed");
        
End Sub Proc4;
    
End Class MyClass;

    
Namespace B
        
Sub Proc1;
        
Begin
            Debug.WriteLine(
"The A.B.Proc1 procedure is executed");
        
End Sub Proc1;
        
        
Sub Proc2;
        
Begin
            Debug.WriteLine(
"The A.B.Proc2 procedure is executed");
        
End Sub Proc2;
    
End Namespace B;

    
Namespace B
        
Sub Proc3;
        
Begin
            Debug.WriteLine(
"The A.B.Proc3 procedure is executed");
        
End Sub Proc3;
    
End Namespace B;
End Namespace A;

Sub Main;
Var
    MyVar: A.MyClass;
Begin
    MyVar := 
New A.MyClass.Create;
    MyVar.Run;
    A.B.Proc3;
End Sub Main;

After executing the example the following messages are displayed in the development environment console:

Unit execution started

The A.B.Proc1 procedure has been completed

The A.B.Proc2 procedure has been completed

The Proc4 procedure has been completed

The A.B.Proc3 procedure has been completed

Unit execution finished

The Imports Statement

The Imports statement imports the namespace contents from units, forms or assemblies, which this compilation unit has references to. This enables the use of unqualified identifiers when calling structures described in the imported namespace.

If an imported namespace has a structure, which identifier matches the identifier defined within the current compilation unit, the priority is given to the locally defined structure rather than to the imported structure.

NOTE. The Imports statement imports all structures declared with the Public access modifier except for nested namespaces. To call nested namespaces and their contents, use qualified identifiers.

Example

Using the Imports statement.

Contents of the first unit:

Namespace Space1
    
Public Sub Proc1;
    
Begin
        Debug.WriteLine(
"The Proc1 procedure is executed from Space1");
    
End Sub Proc1;
    
    
Sub Proc2;
    
Begin
        Debug.WriteLine(
"The Proc2 procedure is executed from Space1");
    
End Sub Proc2;

    
Namespace SubSpace
        
Public Sub SubProc;
        
Begin
            Debug.WriteLine(
"The SubProc procedure is executed from Space1.SubSpace");
            Proc2;
        
End Sub SubProc;
    
End Namespace SubSpace;
End Namespace Space1;

Contents of the second unit:

Imports Space1;
Sub Proc1;
Begin
    Debug.WriteLine(
"The Proc1 procedure is executed from the current unit");
End Sub Proc1;

Sub Main;
Begin
    Space1.Proc1;
    Proc1;
    Space1.SubSpace.SubProc;
End Sub Main;

In the second unit in the assembly inspector a reference to the first unit is added. After executing the example the console window displays the following messages:

Unit execution started

The Proc1 procedure from Space1 has been completed

The Proc1 procedure from the current unit has been completed

The SubProc procedure from Space1.SubSpace has been completed

The Proc2 procedure from Space1 has been completed

Unit execution finished

See also:

Fore Language Guide | Using Variables in Namespaces