Use constant in enum

Hello Community
Is it possible to use a constant in an enumeration?
Use Case:
In a B&R library, constants are used for configuration for various input parameters. To standardize our internal function block, I would like to simplify this so that related constants of a parameter each result in an enumeration.

Thank you for your help.

1 Like

Hi @Pad_Dern Paddern and welcome to our communiy.

To be honest, I haven’t quite understood that yet.

The individual members of an enumeration are (integer) constants. And enumerations in themselves are the better alternative to individual (integer) constants.

The members usually start with 0 and are incremented by 1 but you can break this up by giving them individual numerical constants.

just use the enumeration’s name instead of the integer data type in your function block.

Hi Christoph Hilchenbach
I don’t want to use “hardcoded”/“magic” numbers. Instead I’ve tried to use the constants from the B&R library but I get this error when I use the constant as value:

unfortunatly you can’t do this.

Either you ommit all values and let compiler determine the real constants or use the hardcoded numbers from that library.

I don’t understand that.
If I let the compiler determine the constants, they certainly won’t match the parameter values ​​of the B&R constants.

So the answer is that you need a separate handling to convert the enum values ​​to the constant values? Cumbersome.

Hi,

the problem with that could be that the enum itself is of type “UDINT”. A Constant can be anything…

also, the value of a enum is not a magic number, the same way the value of a constant is not a magic number…

anyways, if you dont want to use the constants directly but use your own enum you would have to put the values in by hand… it is cumbersome but you only have to do it once…

An overview of all contants of an library can be found in the respective capter of the help

best regards

1 Like

if you dont want to use the constants directly but use your own enum you would have to put the values in by hand

or use the all-purpose-tool Python script for that.

I think you don’t understand what I mean by magic number here:
The source of the number in the enum is the magic number, because the constant cannot be used in the enum and thus the reference to the source of this number is missing. Therefore, for me, it’s a magic number. If the B&R constant could be specified directly, the reference would be provided, as it can be found very easily.

In my opinion, it’s absolutely not a good solution to hardcode the value into the enum, and from experience, I no longer trust that a constant from a library/framework never (ever) changes (which changes the behavior due to the block configuration). And as soon as the value changes, the search for the cause of a new problem lasts for days. If the constant could be used in the enum and one day the value of the B&R constant changes, there would be no problem, and if the name of the constant were to change, it would lead to a compile error that would be detectable very early on.

1 Like

This is one option…
So uninstall Automation Studio and only process the text-based projects with other tools? :rofl:

So uninstall Automation Studio and only process the text-based projects with other tools?

no, it is not necessary to uninstall it. All source code files are plain text files so it should not be so much work to translate one text file to another format. Maybe even a editor with automation (e.g. notepad++) can help.

Hello,
the reason this code does not work is because compiler requires enum values to be constant expressions known at compile time, and:

_GLOBAL_CONST unsigned long profLOGGRP_TASK_SWITCH; from AsArProf library is not a constant expression in the sense the compiler needs for initializing an enum.

Or in other words it is not usable here because enums need compile-time constants (e.g., literals, #define, or enum values). Const variables can’t be used in enum initializers unless they’re constexpr (in C++) or #define macros (in C).

You can certainly do something like this:

#define my_profLOGGRP_TASK_SWITCH 1U
and use it in enumeration:
enum{
myEnumElement = my_profLOGGRP_TASK_SWITCH,
};

3 Likes

@Michal_Malek
thanks for the hint.

in fact the IEC constants are not really constants from the compiler’s perspective. They are read-only variables.

yes with C/C++ there is much more possible when using the _REPLACE_CONST directive

Thanks. This solution looks very good.

Just to be over correct, ENUMs are stored as DINT variables. But you are right, ENUMs can be “anything”.
BR Fabian

So there is no way to “fake” this (work-around)?
To use the solution mentiond by Michal Malek and use this behavior in ST?

Then I’ll use a mapping or something because company say: Application (will use the ENUM) has to be ST