I’m getting the error “A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type” in my code at the line,
switch (job_selecter.SelectedValue)
Here’s my code:
private void start()
{
switch (job_selecter.SelectedValue)
{
case 0:
head_seal_label.Text = "Ravager's Seal: Head (8)";
break;
}
}
Could anyone tell me why this is happening and how I can fix it? Thanks!
It seems like what you really want to do is this:
You’ve noted in one of your responses that casting
SelectedValuetostringorintor whatever can cause a null reference exception if you then use it in a switch–which makes perfect sense, because it’s perfectly legal for a combo box to have nothing selected, and you’re going to need to account for that case. If you switch on SelectedIndex, handling -1 will allow you to handle a case of “no selection” specifically.Of course, it’s worth pointing out that switching on
SelectedIndexonly makes sense if the combo box contains a known, unchanging set of values. Adding or removing values will potentially cause the indices of everything in the box to change, thus breaking the switch.