TO_STRING/TO_WSTRING for enumeration variables

If you wish to query the textual identifier of an enumeration component, for example in order to process it further in a text output, add the Attribute 'to_string' above the declaration of the enumeration. In the implementation part you can then apply the conversion function TO_STRING or TO_WSTRING to an enumeration variable or to the components of the enumeration. The name of the enumeration component will then be returned.

TO_STRING/TO_WSTRING for enumeration variables 1:

Attribute 'to_string'

Available from TC3.1 Build 4024

The conversion functions TO_STRING/TO_WSTRING can also be applied to enumerations that are not declared with the attribute 'to_string'. In this case the numerical value of the enumeration component will be returned.

Sample:

Enumeration E_Sample

{attribute 'qualified_only'}
{attribute 'strict'}
{attribute 'to_string'}
TYPE E_Sample :
(
    eInit := 0,
    eStart,
    eStop
);
END_TYPE

Program MAIN

PROGRAM MAIN
VAR
    eSample         : E_Sample;
    nCurrentValue   : INT;
    sCurrentValue   : STRING;
    wsCurrentValue  : WSTRING;
 
    sComponent      : STRING;
    wsComponent     : WSTRING;
END_VAR
nCurrentValue  := eSample; 
sCurrentValue  := TO_STRING(eSample);
wsCurrentValue := TO_WSTRING(eSample);

sComponent     := TO_STRING(E_Sample.eStart);
wsComponent    := TO_WSTRING(E_Sample.eStop);

 

Result of the assignments/conversion functions:

 

Result if the enumeration were not to be declared with the attribute 'to_string':

See also: