How do I convert an integer to an enumerated type?

五迷三道 提交于 2019-11-30 08:04:37
ain

As Ken answered, you just cast it. But to make sure you have correct value you can use code like:

if (ordValue >= Ord(Low(TMyType))) and (ordValue <= Ord(High(TMyType))) then
    enunValue := TMyType(ordValue)
else 
    raise Exception.Create('ordValue out of TMyType range');

You can cast the integer by typecasting it to the enumerated type:

ordValue := Ord(mtSecond);
enumValue := TMyType(ordValue);
user3140262

Take care with casting because it requires full mapping with your ordinal type and integers. For example:

type Size = (Small = 2, Medium = 3, Huge = 10);
var sz: Size;
...
sz := Size(3); //means sz=Medium
sz := Size(7); //7 is in range but gives sz=outbound
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!