Namespace

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.

Syntax

$ NamespaceDeclaration = NAMESPACE ident

NamespaceMemberList;

END NAMESPACE ident ";"

 

$ NamespaceMemberList = [ NamespaceMember { NamespaceMember }]

$ MemberDeclaration = (NamespaceDeclaration | ClassDeclaration | InterfaceDeclaration | VariableDeclaration | ConstDeclaration | ProcedureDeclaration | FunctionDeclaration | EnumDeclaration | DelegateDeclaration)

Description

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 has been completed");

End Sub Proc4;

End Class MyClass;

 

Namespace B

Sub Proc1;

Begin

Debug.WriteLine("The A.B.Proc1 procedure has been completed");

End Sub Proc1;

Sub Proc2;

Begin

Debug.WriteLine("The A.B.Proc2 procedure has been completed");

End Sub Proc2;

End Namespace B;

 

Namespace B

Sub Proc3;

Begin

Debug.WriteLine("The A.B.Proc3 procedure has been completed");

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:

Module 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

Module execution finished

The Imports Statement

Syntax

$ ImportsDeclaration = IMPORTS ident ";"

Description

The Imports statement is used to import 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 operator.

Contents of the first unit:

Namespace Space1

Public Sub Proc1;

Begin

Debug.WriteLine("The Proc1 procedure from Space1 has been completed");

End Sub Proc1;

 

Sub Proc2;

Begin

Debug.WriteLine("The Proc2 procedure from Space1 has been completed");

End Sub Proc2;

 

Namespace SubSpace

Public Sub SubProc;

Begin

Debug.WriteLine("The SubProc procedure from Space1.SubSpace has been completed");

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 from the current unit has been completed");

End Sub Proc1;

 

Sub Main;

Begin

Space1.Proc1;

Proc1;

Space1.SubSpace.SubProc;

End Sub Main;

In the second module in the assembly inspector a reference to the first unit is added. After executing the example the following messages are displayed in the development environment console:

Module 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

Module execution finished

See also:

Descriptions and Syntax Rules | Using Variables in Namespaces