5

I trying to convert from WPF combobox selected value to enumurator it return not valid cast in the runtime otherwise the string and the enum name is matched my code is

Siren.PfundMemberWebServices.Emirates EM = (Siren.PfundMemberWebServices.Emirates)cmbemirate.SelectedValue
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Badr Hussien
  • 149
  • 1
  • 11

3 Answers3

5

To convert a string to an enum you need to use Enum.Parse

Siren.PfundMemberWebServices.Emirates EM = (Siren.PfundMemberWebServices.Emirates)Enum.Parse(typeof(Siren.PfundMemberWebServices.Emirates), cmbemirate.SelectedValue);
Sean
  • 60,939
  • 11
  • 97
  • 136
3

Your question is not complete but the InvalidCastException occurs when an explicit cast is applied. But the type is not in the same path of the type hierarchy. The cast does not succeed.

Use:

Siren.PfundMemberWebServices.Emirates EM = (Siren.PfundMemberWebServices.Emirates)Enum.Parse(typeof(Siren.PfundMemberWebServices.Emirates), cmbemirate.SelectedValue);
1

If you have int values with combobox then you can try:

Siren.PfundMemberWebServices.Emirates EM = (Siren.PfundMemberWebServices.Emirates)Convert.ToInt32(cmbemirate.SelectedValue)
Girish Vadhel
  • 735
  • 1
  • 5
  • 17