how do I cast explicitly ENUM to UINT (or other compatible type) and back UINT to ENUM?
I haven't found dedicated conversion operators.
I'd like to use 'strict' pragma and cast explicitely. Now I have to remove 'strict' and casting happens implicitly, which is bad of course...
Thank you!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2017-12-05
Originally created by: scott_cunningham
I don't know of an ENUM_TO_UINT() or UINT_TO_ENUM() type of conversion.
BUT, you can turn on "Insert with namespace" in the SmartCoding options. This will include the namespace of the enum automatically, avoiding incorrect casting with another enum. At least it avoids issues when two enumerations have the same entry with different values.
Then it's up to you to never do this:
MyEnum:=53;
And instead, do this:
MyEnum:=T_STATUS.IDLE_AFTER_FAULT;
(Of course, you can include the namespace manually without turning on the SmartCoding feature...)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
βAs of CODESYS V3.5 SP7, each enumeration that you add to a project receives the 'strict' attribute automatically in the line above the TYPE declaration. For compiler versions >=3.5.7.0 , this causes compile errors in the following cases:
- Arithmetic operation with variables of the enumeration type
- Assignment of a constant value to a variable of the enumeration type, in which the constant does not correspond to an enumeration value
- Assignment of a non-constant value to a variable of the enumeration type, in which the non-constant has another data type than the enumeration type
Either you delete the {attribute 'strict'} from your ENUM and try again or you better follow the ideology of bulletproof programming, by checking your UINT against different values and then set the ENUM with one of it's members.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Originally created by: P.C.
Dear All,
how do I cast explicitly ENUM to UINT (or other compatible type) and back UINT to ENUM?
I haven't found dedicated conversion operators.
I'd like to use 'strict' pragma and cast explicitely. Now I have to remove 'strict' and casting happens implicitly, which is bad of course...
Thank you!
Originally created by: scott_cunningham
I don't know of an ENUM_TO_UINT() or UINT_TO_ENUM() type of conversion.
BUT, you can turn on "Insert with namespace" in the SmartCoding options. This will include the namespace of the enum automatically, avoiding incorrect casting with another enum. At least it avoids issues when two enumerations have the same entry with different values.
Then it's up to you to never do this:
And instead, do this:
(Of course, you can include the namespace manually without turning on the SmartCoding feature...)
Instead of converting could you just define the ENUM as type UINT instead?
Here an explanation from the help files:
βAs of CODESYS V3.5 SP7, each enumeration that you add to a project receives the 'strict' attribute automatically in the line above the TYPE declaration. For compiler versions >=3.5.7.0 , this causes compile errors in the following cases:
- Arithmetic operation with variables of the enumeration type
- Assignment of a constant value to a variable of the enumeration type, in which the constant does not correspond to an enumeration value
- Assignment of a non-constant value to a variable of the enumeration type, in which the non-constant has another data type than the enumeration type
Either you delete the {attribute 'strict'} from your ENUM and try again or you better follow the ideology of bulletproof programming, by checking your UINT against different values and then set the ENUM with one of it's members.