如何将整数转换为枚举类型? [英] How do I convert an integer to an enumerated type?

查看:185
本文介绍了如何将整数转换为枚举类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know how to convert an enumerated type to an integer.

type
  TMyType = (mtFirst, mtSecond, mtThird); 

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

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

But how do I do the inverse operation and convert an integer to an enumerated type?

推荐答案

正如Ken回答,你只是投了。但是要确保你有正确的价值,你可以使用如下代码:

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');

这篇关于如何将整数转换为枚举类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆