4

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

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?

Community
  • 1
  • 1
Robo
  • 4,588
  • 7
  • 40
  • 48

1 Answers1

3

@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
  • 134,889
  • 20
  • 356
  • 483