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?
- Global namespace and local namespace
- Namespaces naming conventions
- Accessing elements of a namespace
- Nested namespaces
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:
- Standard POUs: functions, function blocks and programs(Namespaces are not supported for safety-related POUs.)
- User-defined data types
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:
- The name must be a valid identifier according to IEC 61131-3 (2013-02).
- Extended identifier characters for namespaces are not allowed.
- A namespace name can be up to 90 characters in length.
- The namespace can be empty (language element is a member of the global namespace).
- Special characters are not allowed, e.g. - , ; : $ ! ? \ / < > and others. Only the _ character is allowed.
- Namespace names are case-insensitive.
Note
For C# POUs, the Microsoft C# compiler handles namespaces as case sensitive. - The namespace identifier '_.' is reserved for elements of the global namespace.
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:
- Full qualified name For accessing the language element outside of the namespace where it is defined, the full qualified name of the element must be used (see also the section "Nested namespaces" below). A full qualified name consists of a single namespace identifier or a list of identifiers separated by a . (dot) followed by the short name of the element. For example, 'NamespaceSample.MyElement' is a full qualified name where 'NamespaceSample' refers to the namespace name and 'MyElement' to the language element inside the namespace.
Note
PLCnext Engineer supports the definition of USING declarations. The USING declaration allows you to access the member of a namespace without specifying its namespace identifier. For details how to use this functionality for user-defined data types and POUs, see the topics "Defining Namespaces for POUs" and "Defining Namespaces for User-defined Data Types". - Short (local) nameThe short (local) name of an element is the element name without the namespace identifier ('MyElement' in the example above). The access to an element using its short name is only possible if the following conditions are met:
- The language element is a member of the global namespace.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.
- The language element has the same namespace as its containing element.
- The language element is introduced into another namespace by using a USING declaration (see the note above).
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