4
基本上與此相同的問題,但在德爾福棱鏡:演員int值的枚舉德爾福棱鏡
我設法從一個字符串做:
YourEnum := Enum.Parse(TypeOf(YourEnum), "mystr") as YourEnum
但我試過以下,並得到類型不匹配錯誤:
YourEnum := 3 as YourNum
任何想法語法是什麼f或將int轉換爲Enum?
基本上與此相同的問題,但在德爾福棱鏡:演員int值的枚舉德爾福棱鏡
我設法從一個字符串做:
YourEnum := Enum.Parse(TypeOf(YourEnum), "mystr") as YourEnum
但我試過以下,並得到類型不匹配錯誤:
YourEnum := 3 as YourNum
任何想法語法是什麼f或將int轉換爲Enum?
@Robo的sintax轉換爲一個int到枚舉
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.
謝謝,非常全面的回答。 – Robo 2010-03-05 04:25:24