Can I be sure that delphi assign integer number in a ascending sequence to any enumeration a my create ?
type
TMyType = (mtFirst, mtSecond, mtThird);
there for sure mtFirts is always TmyType(1)
???
I try to write code like myTypeselection := TMyType(MyRadiogroup.ItemIndex+1);
But I fail that the values Integer numbers are somehow mixed .
The documentation has the answer:
So your belief that numbering starts from one is in fact incorrect. It is the case the
ord(mtFirst)=0
,ord(mtSecond)=1
and so on.Which means your code should read:
Because radio group indexing is also zero based.
In my own code, I use the following generic class to perform operations like this:
With that at hand your code becomes:
If you don't specify values for your enumeration values, the compiler will start with zero, so this
is equivalent to
If you use the correct start value 0, casting from integer to your enumeration and back is safe.