delphi - 如何将整数转换为枚举类型?

标签 delphi casting integer enumeration

我知道如何将枚举类型转换为整数。

type
  TMyType = (mtFirst, mtSecond, mtThird); 

var 
  ordValue:integer;
  enumValue:TMyType;
...
ordValue:= Ord(mtSecond); // result is 1

但是如何进行逆运算并将整数转换为枚举类型呢?

最佳答案

正如肯回答的那样,你只是施放它。但为了确保你有正确的值,你可以使用如下代码:

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

关于delphi - 如何将整数转换为枚举类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8732391/

相关文章:

C++ 类将对象转换为整数

mysql - MySQL 中的 tinyint、smallint、mediumint、bigint 和 int 有什么区别?

delphi - Delphi 2007 中的 HelpInsight 文档

xml - poAsyncLoad选项对TXMLDocument起作用吗?

c# - 为什么 Enumerable.Cast 引发 InvalidCastException?

ios - swift : Cast wrapped value

c++ - 在不更改数据类型的情况下可逆地组合两个 uint32_t

delphi - 如何从字符串列表中解析多行

delphi - 可以生成 PDF/A 兼容文件的报告生成器

postgresql - 如何将 varchar 转换为 bool postgresql