0

In my main form code, I've added this:

public static ShoeStyle selectedStyle;

ShoeStyle is the name of an enum I have in a class. I want selectedStyle to contain the name of whatever I select in my combo box. Like this

private void cmbShoeList_SelectedIndexChanged(object sender, EventArgs e)
{
    selectedStyle = (ShoeStyle)cmbShoeList.SelectedValue;                       
}

But when I run the program, it gives me the error:

specified cast is not valid.

I'm not sure how I can fix this. I've heard of the term cast before but am unfamiliar with it

Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28
AbrahamKMS
  • 85
  • 5
  • 1
    Try following : (ShoeStyle)Enum.Parse(typeof(ShoeStyle), cmbShoeList.SelectedValue) – jdweng Apr 16 '20 at 15:52
  • The `(ShoeStyle)` is the cast. A cast is when you try to take something of one type and make it another type. In this case, you likely have a string type (`SelectedValue`) and you are trying to cast it as the `ShoeStyle` type. There's nothing telling the compiler how to do that, so that's why you get the error. – Heretic Monkey Apr 16 '20 at 15:55
  • @jdweng awesome, your code worked. Thanks – AbrahamKMS Apr 16 '20 at 15:58

0 Answers0