4

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

2
  • 9
    Move your eyes to the right "Related" bar. Commented Jul 1, 2013 at 4: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# Commented Jul 1, 2013 at 4:54

1 Answer 1

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; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.