<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to explicit cast ENUM to UINT and vice versa</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/3c10041dae/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/Engineering/thread/3c10041dae/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/Engineering/thread/3c10041dae/</id><updated>2025-08-21T23:21:53.052000Z</updated><subtitle>Recent posts to explicit cast ENUM to UINT and vice versa</subtitle><entry><title>explicit cast ENUM to UINT and vice versa</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/3c10041dae/?limit=25#4370" rel="alternate"/><published>2025-08-21T23:21:53.052000Z</published><updated>2025-08-21T23:21:53.052000Z</updated><author><name>davemansell</name><uri>https://forge.codesys.com/u/davemansell/</uri></author><id>https://forge.codesys.com84ef45738c2e3c4a2942061dd26f1eb34f98e9b9</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;This is from sometime ago but below might help you could do this via POINTERS.&lt;/p&gt;
&lt;p&gt;Make sure you protect for NULL pointers etc if you take this approach.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;DECLARATION&lt;/span&gt;
&lt;span class="n"&gt;myEnumValue&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;enumMyEnum&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;u8&lt;/span&gt;&lt;span class="w"&gt;           &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;USINT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;pmyEnumValue&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;POINTER&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;TO&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;enumMyEnum&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;pu8&lt;/span&gt;&lt;span class="w"&gt;          &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;POINTER&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;TO&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;USINT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;CODE&lt;/span&gt;
&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;pmyEnumValue&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;pu8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;myEnumValue&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;pmyEnumValue&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;dereference&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;the&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;pointer&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;</summary></entry><entry><title>explicit cast ENUM to UINT and vice versa</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/3c10041dae/?limit=25#54a3" rel="alternate"/><published>2025-08-21T23:21:43.868000Z</published><updated>2025-08-21T23:21:43.868000Z</updated><author><name>davemansell</name><uri>https://forge.codesys.com/u/davemansell/</uri></author><id>https://forge.codesys.comf4f7f715db7b39722450d278c42a6e84264b7e96</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;This is from sometime ago but below might help you could do this via POINTERS.&lt;/p&gt;
&lt;p&gt;Make sure you protect for NULL pointers etc if you take this approach.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;DECLARATION&lt;/span&gt;
&lt;span class="n"&gt;myEnumValue&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;enumMyEnum&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;u8&lt;/span&gt;&lt;span class="w"&gt;           &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;USINT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;pmyEnumValue&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;POINTER&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;TO&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;enumMyEnum&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;pu8&lt;/span&gt;&lt;span class="w"&gt;          &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;POINTER&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;TO&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;USINT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;CODE&lt;/span&gt;
&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;pmyEnumValue&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;pu8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;myEnumValue&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;pmyEnumValue&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;dereference&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;the&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;pointer&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;</summary></entry><entry><title>explicit cast ENUM to UINT and vice versa</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/3c10041dae/?limit=25#d595" rel="alternate"/><published>2021-03-11T17:08:03.595000Z</published><updated>2021-03-11T17:08:03.595000Z</updated><author><name>laruso</name><uri>https://forge.codesys.com/u/laruso/</uri></author><id>https://forge.codesys.com34b91afdf4883ca99e84ab97de805a874923496f</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Here an explanation from the help files:&lt;/p&gt;
&lt;p&gt;„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 &amp;gt;=3.5.7.0 , this causes compile errors in the following cases:&lt;br/&gt;
-   Arithmetic operation with variables of the enumeration type&lt;br/&gt;
-   Assignment of a constant value to a variable of the enumeration type, in which the constant does not correspond to an enumeration value&lt;br/&gt;
-   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&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>explicit cast ENUM to UINT and vice versa</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/3c10041dae/?limit=25#fcf9" rel="alternate"/><published>2020-02-27T13:47:41.757000Z</published><updated>2020-02-27T13:47:41.757000Z</updated><author><name>Comingback4u</name><uri>https://forge.codesys.com/u/comingback4u/</uri></author><id>https://forge.codesys.com6319e9c899053559bb2cc9c8685dbaa97394f7c4</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Instead of converting could you just define the ENUM as type UINT instead?&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;TYPE&lt;/span&gt; &lt;span class="n"&gt;MyEnum&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="n"&gt;EnumMember1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="n"&gt;EnumMember2&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;UINT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;END_TYPE&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;</summary></entry><entry><title>explicit cast ENUM to UINT and vice versa</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/3c10041dae/?limit=25#2837" rel="alternate"/><published>2020-02-27T13:47:41.680000Z</published><updated>2020-02-27T13:47:41.680000Z</updated><author><name>Anonymous</name><uri>https://forge.codesys.com/u/*anonymous/</uri></author><id>https://forge.codesys.comba838b9468070fb9ed622584d91be5b79a29fd4b</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;&lt;em&gt;Originally created by:&lt;/em&gt; scott_cunningham&lt;/p&gt;
&lt;p&gt;I don't know of an ENUM_TO_UINT() or UINT_TO_ENUM() type of conversion.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;Then it's up to you to never do this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;MyEnum&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;53&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;And instead, do this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;MyEnum&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;T_STATUS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IDLE_AFTER_FAULT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;(Of course, you can include the namespace manually without turning on the SmartCoding feature...)&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>explicit cast ENUM to UINT and vice versa</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/3c10041dae/?limit=25#5e66" rel="alternate"/><published>2020-02-27T13:47:41.631000Z</published><updated>2020-02-27T13:47:41.631000Z</updated><author><name>Anonymous</name><uri>https://forge.codesys.com/u/*anonymous/</uri></author><id>https://forge.codesys.com61d5471c0acce3a0e006f11f9d88f1ccb95819c7</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;&lt;em&gt;Originally created by:&lt;/em&gt; P.C.&lt;/p&gt;
&lt;p&gt;Dear All,&lt;/p&gt;
&lt;p&gt;how do I cast explicitly ENUM to UINT (or other compatible type) and back UINT to ENUM?&lt;br/&gt;
I haven't found dedicated conversion operators.&lt;br/&gt;
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...&lt;/p&gt;
&lt;p&gt;Thank you!&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>