Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign int value to the custom enum property [duplicate]

Tags:

c#

.net

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

like image 358
panjo Avatar asked Oct 15 '25 07:10

panjo


1 Answers

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;
like image 178
Richard Schneider Avatar answered Oct 21 '25 01:10

Richard Schneider



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!