Defining Namespaces for POUs
Further Info
For a description what namespaces are, how to access elements of a namespace, nested namespaces, etc., see the topic "General Information on Namespaces". |
This topic contains the following sections:
- General notes on defining namespaces for POUs
- Defining a namespace in the 'Settings' editor of the POU
- Defining a namespace in the COMPONENTS area
- Defining 'Usings' for a POU
- Representation of FU and FB POUs in the graphic editor depending on namespace
General notes on defining namespaces for POUs
- Namespaces are not supported for safety-related POUs.
- Namespaces are not supported for safety-related types yet (this will be supported in future).
- Observe the naming conventions for namespaces when defining a new namespace.
- If a namespace is defined for a language element or an existing namespace identifier is renamed, the element name must be adapted accordingly at all locations where it is used in the project. For example, if you define a namespace for a program POU and the POU is already instantiated in the 'Tasks and Events' editor, the compiler reports an error because the instantiated program POU is unknown.
Defining a namespace in the 'Settings' editor of the POU
Note
If the namespace for a POU is changed in the 'Settings' editor, the namespace is automatically updated in the COMPONENTS area and vice versa. |
To define a namespace for a POU in the 'Settings' editor (not available for safety-related POUs), proceed as follows:
- In the COMPONENTS area, double-click the POU for which you want to define a namespace and open its 'Settings' editor in the editors area.
- In the 'Namespace' field, either enter a new namespace name or choose an existing name from the selection list (the selection list provides all namespaces defined in your project). Confirm the name with <Enter>.
To create a nested namespace, enter the namespace names separated by a . (dot), e.g., NamespaceA.NamespaceB.NamespaceC.In the COMPONENTS area, the POU name is automatically updated displaying the full qualified name of the POU.
Defining a namespace in the COMPONENTS area
To define a namespace for a POU in the COMPONENTS area, proceed as follows:
- In the COMPONENTS area, select the POU and press <F2>. (If a namespace is already defined for a POU, pressing <F2> preselects only the POU name without the namespace).
- Enter the namespace by prefixing the POU name with the namespace name followed by a . (dot), e.g., NamespaceA.MyFB.
To create a nested namespace, enter the namespace names separated by a . (dot), e.g., NamespaceA.NamespaceB.NamespaceC.MyFB.
To delete a namespace for a POU, either delete the namespace name entered in the 'Namespace' field in the 'Settings' editor of the POU or remove the namespace name in the POU name in the COMPONENTS area.
Defining 'Usings' for a POU
To use a POU outside of the namespace where it is defined, you usually have to specify the full qualified name of the POU. For example, if you want to instantiate the FB POU 'NamespaceA.MyFB' in a program POU, you have to specify the full qualified name 'NamespaceA.MyFB' in the program POU. In order not to have to enter the full qualified name every time, you can add the namespace of the FB POU to the list of 'Usings' of the program POU. This allows you to use the short name (without namespace identifier) of the FB POU in the calling program POU (see also the example below).
When defining 'Usings' for a POU, note the following:
- 'Usings' are an optional feature to simplify the use of language elements and to improve the readability of your code when using namespaces. They are particularly helpful, especially when you have language elements which have many levels of nested namespaces.
- If a namespace is added to the 'Usings' list of a POU, the elements that belong to that namespace are displayed two times (with its full qualified name and short name) at the locations where the element can be accessed as shown in the following figure. Here, the selection list in the 'Variables' table (position 1) displays the POU FB1 two times (with and without namespace) because 'NamespaceB' (FB1 is a member of that namespace) is added to the 'Usings' list of the POU 'NamespaceA.Main' (position 2).
- Even with defined 'Usings', you can use (specify) the full qualified name of a language element.
To add a namespace to the 'Usings' list of a POU, proceed as follows:
- In the COMPONENTS area, double-click the POU for which you want to add a namespace to the 'Usings' list and open its 'Settings' editor in the editors area.
- In the 'Usings' table, left-click into the combobox below the table header and select a namespace from the list or enter the name of the desired namespace (Intellisense is supported). Confirm the selection with <Enter> or clicking outside of the combobox. The selection list contains all namespaces (including the nested namespaces) that are available in your project. (When you confirm the selection, a new empty entry is added to the table).
Repeat this step for all namespaces you want to add.
Example of the 'Usings' behavior
To remove a namespace from the 'Usings' list, select the entry in the list and press <Del>.
Representation of FU and FB POUs in the graphic editor depending on namespace
In the graphic editor, the function block type is always displayed within the block symbol of an instantiated function or function block POU. Depending on the namespace setting for the instantiated POU and the 'Using' declaration for the calling POU, the block type is prefixed with an additional character as shown in the following examples for an FB POU:
The tooltip of the block symbol always shows the full qualified name of the function block type.