可以使用类似的东西将枚举转换为字符串:
uses
TypInfo;
type
Language = (Delphi,Delphi_Prism,CBuilder);
var
StrLanguage : String;
begin
StrLanguage := GetEnumName(TypeInfo(Language),integer(Delphi)) ;
end;
Run Code Online (Sandbox Code Playgroud)
(摘自theroadtodelphi)
是否可以对具有自定义值的枚举执行相同操作?
像这样的东西:
type
THotkey = (hkShift= 1, hkSpace= 3, hkEnter= 6);
Run Code Online (Sandbox Code Playgroud)
作为一种解决方法,我使用占位符来跳过未使用的枚举.
然而,这不是很好,如果我不得不跳过巨大的差距是有问题的.
type
THotkeys = (hkShift, hkUnused1, hkSpace, hkUnused2, hkUnused3, hkEnter);
Run Code Online (Sandbox Code Playgroud)
在您的特定用例中,您可以使用与枚举相关的数组,因为具有特定值的枚举常量不具有文档中所述的RTTI:
没有特定值的枚举常量具有RTTI:
Run Code Online (Sandbox Code Playgroud)type SomeEnum = (e1, e2, e3);而具有特定值的枚举常量(如下所示)没有RTTI:
Run Code Online (Sandbox Code Playgroud)type SomeEnum = (e1 = 1, e2 = 2, e3 = 3);
你可以这样解决这个问题:
type
THotkey = (hkShift, hkSpace, hkEnter);
THotkeyValues: array[Thotkey] of Integer = (1,3,6);
Run Code Online (Sandbox Code Playgroud)
用法:
ShiftKeyValue := THotkeyValues[hkShift];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
843 次 |
| 最近记录: |