5

I have property

public Enums.CustomEnumProp MyEnum { get; set; } 

which is of type CustomEnumProp

public enum CustomEnumProp { A = 1, B = 2, C = 3}

and I need to use passed int value as a user selection and assign it to the MyEnum property.

forexampe: is user is selected 2 from combobox then assign this int to the MyEnum.

Thanks

panjo
  • 3,467
  • 11
  • 48
  • 82
  • 9
    Move your eyes to the right "Related" bar. – zerkms Jul 01 '13 at 04:54
  • 1
    Kindly use the search before making a new question, as you can see on the right side the very first result is your answer [Cast int to enum in C#](http://stackoverflow.com/questions/29482/cast-int-to-enum-in-c-sharp) – Prix Jul 01 '13 at 04:54

1 Answers1

5

Just cast the int to the enum.

o.MyEnum = (CustomEnumProp) myInt;

You can also use the Enum.IsDefined method to check that the int is valid.

if (Enum.IsDefined(typeof(CustomEnumProp), myInt))
   o.MyEnum = (CustomEnumProp) myInt;
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73