-

General Information on Namespaces

Further Info
This topic provides general information on namespaces. For details how to define namespaces for POUs and user-defined data types in PLCnext Engineer, see the topics "Defining Namespaces for POUs" and "Defining Namespaces for User-defined Data Types".

This topic contains the following sections:

What are namespaces?

Namespaces allow you to organize and group language elements to make them unique within the PLCnext Engineer project. Namespaces help you to ensure that the language element names do not collide with other names. By using namespaces you can have more than one language element (e.g. a POU) with the same name in your project, thus avoiding naming collisions. In PLCnext Engineer, you can use namespaces for the following language elements:

Assume that you have two libraries both containing an FB POU named MyFB. When you add both libraries to your project, a compiler error is reported due to the naming collision (as shown in the first figure in the following example). With namespaces this conflict can be resolved because a namespace allows the specific access to a POU (see the second figure in the following example).

Example of a naming conflict without namespaces and resolved conflict using namespaces

Note
Namespaces are not mandatory for your project. Namespaces are an optional extension that you can use to organize the program code in your project.

Global namespace and local namespace

A language element that is not defined in an explicit namespace is part of the global namespace. This means, all elements that are not bound to an explicit namespace have global scope in your PLCnext Engineer project. Global scope means any element in the global scope can be accessed anywhere in the project. For example, all standard (non-safety related) functions and function blocks provided by default in PLCnext Engineer are elements of the global namespace (see pos. 1 in the figure below). The names of the elements in the global namespace can be used exactly as they are defined (observe also the following note). By default, all elements added or declared in your project will become part of the global namespace.

Note
Language elements that are a member of the global namespace can be accessed via their short name or by specifying the global namespace identifier '_.' followed by the short name. For example, the FB DERIVAT can be accessed by using the name DERIVAT or _.DERIVAT.

Note
IEC standard data types are accessed always without the global namespace identifier.

In contrast to the elements of a global namespace, the elements of a local namespace are bound to that namespace, i.e., the elements are members of that namespace. The elements of a local namespace have local scope in the project (see pos. 2 in the following figure; POUs are elements of the namespace 'NamespaceA'). Scope, in this context, refers to how the element is accessed in the project. Local means the element names can be accessed only inside of the namespace where they are defined by their short (local) name (element name without namespace identifier). To access the element outside of the namespace where it is defined, the full qualified name must be used.

Further Info
See the section "Accessing elements of a namespace" below how to access an element of the global or a local namespace.

Namespaces naming conventions

The following conventions apply to the identifiers of namespaces:

Accessing elements of a namespace

A language element that is a member of a namespace has a full qualified name (long name) and a short (local) name.

The access to an element of a namespace can be done with both types of names:

Nested namespaces

Namespaces can be nested hierarchically, i.e., a namespace can be nested inside another namespace. The access to an element of a nested namespace is done via its full qualified name as follows:

Syntax:      NamespaceA.NamespaceB.NamespaceC.LanguageElement

Example of nested namespaces and the access to their members