# Use constant in enum

**URL:** <https://community.br-automation.com/t/use-constant-in-enum/7180>\
**Category:** Programming\
**Tags:** structured-text\
**Created:** [June 4, 2025, 5:16am UTC](https://community.br-automation.com/t/use-constant-in-enum/7180 "2025-06-04T05:16:02Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paddern](https://avatars.discourse-cdn.com/v4/letter/p/FF8800/32.png) [@Paddern](https://community.br-automation.com/u/Paddern)\
**Post date:** [June 4, 2025, 5:16am UTC](https://community.br-automation.com/t/use-constant-in-enum/7180/1 "2025-06-04T05:16:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![christoph.hilchenbac](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/christoph.hilchenbac/32/423_2.png) [@christoph.hilchenbac](https://community.br-automation.com/u/christoph.hilchenbac)\
**Post date:** [June 4, 2025, 7:43am UTC](https://community.br-automation.com/t/use-constant-in-enum/7180/2 "2025-06-04T07:43:34Z")

</div>

Hi @Paddern 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.

 ![image](https://europe1.discourse-cdn.com/brcommunity/original/2X/9/9892ffb4b202af53fadaea0ab70cd96a84ced538.png)

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

---

<div class="post-metadata">

**Author:** ![Paddern](https://avatars.discourse-cdn.com/v4/letter/p/FF8800/32.png) [@Paddern](https://community.br-automation.com/u/Paddern)\
**Post date:** [June 4, 2025, 8:04am UTC](https://community.br-automation.com/t/use-constant-in-enum/7180/3 "2025-06-04T08:04:43Z")

</div>

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:

 ![image](https://europe1.discourse-cdn.com/brcommunity/original/2X/e/e60d892c9c883c5c435b27c7462bc03b36744672.png)

---

<div class="post-metadata">

**Author:** ![christoph.hilchenbac](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/christoph.hilchenbac/32/423_2.png) [@christoph.hilchenbac](https://community.br-automation.com/u/christoph.hilchenbac)\
**Post date:** [June 4, 2025, 8:24am UTC](https://community.br-automation.com/t/use-constant-in-enum/7180/4 "2025-06-04T08:24:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Paddern](https://avatars.discourse-cdn.com/v4/letter/p/FF8800/32.png) [@Paddern](https://community.br-automation.com/u/Paddern)\
**Post date:** [June 4, 2025, 8:38am UTC](https://community.br-automation.com/t/use-constant-in-enum/7180/5 "2025-06-04T08:38:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Stefan\_Holz](https://avatars.discourse-cdn.com/v4/letter/s/FF8800/32.png) [@Stefan\_Holz](https://community.br-automation.com/u/Stefan_Holz)\
**Post date:** [June 4, 2025, 9:04am UTC](https://community.br-automation.com/t/use-constant-in-enum/7180/6 "2025-06-04T09:04:12Z")

</div>

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](https://help.br-automation.com/#/en/4/libraries%2Fasarprof%2Fdatatypes%2Fconstants_asarprof.html)

best regards

---

<div class="post-metadata">

**Author:** ![christoph.hilchenbac](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/christoph.hilchenbac/32/423_2.png) [@christoph.hilchenbac](https://community.br-automation.com/u/christoph.hilchenbac)\
**Post date:** [June 4, 2025, 12:49pm UTC](https://community.br-automation.com/t/use-constant-in-enum/7180/7 "2025-06-04T12:49:33Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Paddern](https://avatars.discourse-cdn.com/v4/letter/p/FF8800/32.png) [@Paddern](https://community.br-automation.com/u/Paddern)\
**Post date:** [June 4, 2025, 1:19pm UTC](https://community.br-automation.com/t/use-constant-in-enum/7180/8 "2025-06-04T13:19:14Z")

</div>

> [@Stefan\_Holz](#):
>
> also, the value of a enum is not a magic number, the same way the value of a constant is not a magic number…

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.

---

<div class="post-metadata">

**Author:** ![Paddern](https://avatars.discourse-cdn.com/v4/letter/p/FF8800/32.png) [@Paddern](https://community.br-automation.com/u/Paddern)\
**Post date:** [June 4, 2025, 1:20pm UTC](https://community.br-automation.com/t/use-constant-in-enum/7180/9 "2025-06-04T13:20:45Z")

</div>

> [@christoph.hilchenbac](#):
>
> > 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.

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

---

<div class="post-metadata">

**Author:** ![christoph.hilchenbac](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/christoph.hilchenbac/32/423_2.png) [@christoph.hilchenbac](https://community.br-automation.com/u/christoph.hilchenbac)\
**Post date:** [June 4, 2025, 1:28pm UTC](https://community.br-automation.com/t/use-constant-in-enum/7180/10 "2025-06-04T13:28:51Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Michal\_Malek](https://avatars.discourse-cdn.com/v4/letter/m/FF8800/32.png) [@Michal\_Malek](https://community.br-automation.com/u/Michal_Malek)\
**Post date:** [June 4, 2025, 1:54pm UTC](https://community.br-automation.com/t/use-constant-in-enum/7180/11 "2025-06-04T13:54:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![christoph.hilchenbac](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/christoph.hilchenbac/32/423_2.png) [@christoph.hilchenbac](https://community.br-automation.com/u/christoph.hilchenbac)\
**Post date:** [June 4, 2025, 2:09pm UTC](https://community.br-automation.com/t/use-constant-in-enum/7180/12 "2025-06-04T14:09:56Z")

</div>

@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](https://help.br-automation.com/#/en/4/programming%2Fprograms%2Fansic%2Fprogrammingmodel_programs_ansic_lang_replaceconst.html) directive

---

<div class="post-metadata">

**Author:** ![Paddern](https://avatars.discourse-cdn.com/v4/letter/p/FF8800/32.png) [@Paddern](https://community.br-automation.com/u/Paddern)\
**Post date:** [June 4, 2025, 2:27pm UTC](https://community.br-automation.com/t/use-constant-in-enum/7180/13 "2025-06-04T14:27:42Z")

</div>

> [@Michal\_Malek](#):
>
> #define my\_profLOGGRP\_TASK\_SWITCH 1U  
> and use it in enumeration:  
> enum{  
> myEnumElement = my\_profLOGGRP\_TASK\_SWITCH,  
> };

Thanks. This solution looks very good.

---

<div class="post-metadata">

**Author:** ![fabian.pries](https://avatars.discourse-cdn.com/v4/letter/f/FF8800/32.png) [@fabian.pries](https://community.br-automation.com/u/fabian.pries)\
**Post date:** [June 5, 2025, 4:55am UTC](https://community.br-automation.com/t/use-constant-in-enum/7180/14 "2025-06-05T04:55:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Paddern](https://avatars.discourse-cdn.com/v4/letter/p/FF8800/32.png) [@Paddern](https://community.br-automation.com/u/Paddern)\
**Post date:** [June 5, 2025, 9:29am UTC](https://community.br-automation.com/t/use-constant-in-enum/7180/15 "2025-06-05T09:29:21Z")

</div>

> [@Michal\_Malek](#):
>
> #define my\_profLOGGRP\_TASK\_SWITCH 1U

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

---

<div class="post-metadata">

**Author:** ![rudolf.sila](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/rudolf.sila/32/6726_2.png) [@rudolf.sila](https://community.br-automation.com/u/rudolf.sila)\
**Post date:** [July 23, 2025, 11:26am UTC](https://community.br-automation.com/t/use-constant-in-enum/7180/18 "2025-07-23T11:26:24Z")

</div>

Hello,  
Maybe you could create a prebuilt script where this enumeration is defined?  
Probably the best approach would be to create a `.typ` file where you define this variable, with its value read directly from the library.
