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.
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.
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:
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.
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
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.
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,
};