views:

414

answers:

1

Basically the same as this question, but in Delphi Prism:

http://stackoverflow.com/questions/29482/cast-int-to-enum-in-c

I manage to do it from a string:

YourEnum := Enum.Parse(TypeOf(YourEnum), "mystr") as YourEnum

But I tried the following, and get a type mismatch error:

YourEnum := 3 as YourNum

Any ideas what the syntax is for converting int to Enum?

+1  A: 

@Robo , the sintax for convert an int to Enum is

YourEnumVar := YourEnum(3);

or

YourEnumVar := Object(3) as YourEnum;

see this sample

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.
RRUZ
Thank you, a very comprehensive answer.
Robo