Namespaces

A namespace is a unique name that represents a group of related classes, subroutines, functions, and other (nested) namespaces. You can think of it as an abstract container for these members. Its primary purpose is to prevent naming collisions between classes.

Two namespaces are supplied with Synergy/DE:

along with an empty namespace called synglobal. All classes that are distributed with Synergy/DE are in one of these two namespaces. (See System-Supplied Classes for more information about these classes.)

(traditional Synergy only) The SYNDEFNS environment variable specifies the name of the default namespace, along with other namespaces that you want to import by default. (You can specify multiple namespaces with SYNDEFNS; the first one in the list is considered the default.) If SYNDEFNS is not defined, synglobal is the default namespace. Subroutines and functions that have no declared namespace are added to the default namespace. You do not need to import the default namespace or any namespace specified with SYNDEFNS because they are imported automatically. (Note that the default namespace cannot contain classes, only subroutines and functions.)

Creating a namespace

1. Add a namespace declaration to your source file using the NAMESPACE statement. (See NAMESPACE-ENDNAMESPACE.)
2. Add one or more class declarations within the namespace. (See Creating a class.) You can also add structures, functions, and subroutines to the namespace.

A namespace can be partially defined in multiple files.

Importing a namespace

Importing enables you to use classes in a namespace defined in another file without having to fully qualify them. The System and Synergex.SynergyDE namespaces, as well as any namespaces defined with SYNDEFNS, are imported automatically to all files. Namespaces defined in the current file, even if they occur later in the file, are available as well. You can use the IMPORT statement to import other namespaces into specific files.

You can import namespaces from other source files in the compilation unit and from prototype (.dbp) files that are available to the compilation unit. To make a prototype file available, specify its location with the -qimpdir compiler option or the SYNIMPDIR environment variable. (The directories listed with -qimpdir are searched first.)

For example, say you have the namespace MyNamespace, which contains a class MyClass and a method MyMethod. You can use MyMethod in any file within the compilation unit (or any file that has access to the MyNamespace prototype) like this:

MyNamespace.MyClass.MyMethod()

However, if you import MyNamespace, you can then use MyMethod like this:

import MyNamespace
.
.
.
MyClass.MyMethod()

The scope of an IMPORT statement is limited to the file in which it occurs, so you must add an IMPORT statement to every file in which you wish to take advantage of the abbreviated syntax.

For more information, see