基本上与这个问题相同,但在Delphi Prism中:
我设法从字符串中执行此操作:
YourEnum := Enum.Parse(TypeOf(YourEnum), "mystr") as YourEnum
但我尝试了以下操作,并出现类型不匹配错误:
YourEnum := 3 as YourNum
任何想法将int转换为Enum的语法是什么?
答案 0 :(得分:3)
@Robo,将int转换为Enum的语法是
YourEnumVar := YourEnum(3);
或
YourEnumVar := Object(3) as YourEnum;
参见此示例
namespace ConsoleAppEnumsDelphiPrism;
interface
type
Language = (Delphi=1,Delphi_Prism,CBuilder);
ConsoleApp = class
public
class method Main;
end;
implementation
class method ConsoleApp.Main;
var
MyEnum : Language;
begin
// String to Enum
MyEnum := Language(Enum.Parse(typeof(Language), 'Delphi_Prism'));
Console.WriteLine(MyEnum.ToString());//Print Delphi_Prism
// Int to Enum
MyEnum:=Language(2);
Console.WriteLine(MyEnum.ToString());//Print Delphi_Prism
// Int to Enum using "as"
MyEnum:= Object(1) as Language;
Console.WriteLine(MyEnum.ToString());//Print Delphi
Console.ReadKey();
end;
end.