0

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!

5
  • 5
    What type is job_selector.SelectedValue ? Commented Sep 1, 2011 at 16:02
  • 4
    What type is job_selecter.SelectedValue? Commented Sep 1, 2011 at 16:02
  • 3
    What type is job_selecter.SelectedValue? Commented Sep 1, 2011 at 16:03
  • 2
    job_selecter is a combobox within visual studio that contains a bunch of strings. i don't know what type it is... i'm sorry. would it be a string? Commented Sep 1, 2011 at 16:06
  • Tangentially, the Ravager's Seal only gives a +16 bonus to Lightning Resistance; hardly worth the 23000 chaos shards it costs to purchase from Rupert the Freebooter. Commented Sep 1, 2011 at 16:30

5 Answers 5

5

job_selecter.SelectedValue is probably an object.

 private void start()
    {
        int index = (int)job_selecter.SelectedValue;
        switch (index )
        {
            case 0:
                head_seal_label.Text = "Ravager's Seal: Head (8)";
                break;
        }
    }
Sign up to request clarification or add additional context in comments.

4 Comments

oops, i accidentally responded to the wrong comment below. i added (int) as you said up above, but it told me i had a NullReferenceException was unhandled error. does that mean it wasn't the right type?
never mind. I missed a line you typed in, and it worked. Thanks so much!
okay, so now when i tried adding additional cases, i get the NullReferenceException was unhandled error again on the int index = (int)job_selecter.SelectedValue; line. Why is this?
@Ben it's because there is no selected value in the combobox when it evaluates that expression, or job_selecter itself is not set to an object (trying to reference a property such as SelectedValue of a NULL object results in a NullReferenceException)
3

It seems like what you really want to do is this:

switch(job_selecter.SelectedIndex)
{
    case 0:
        // do whatever
        break;

    default:
        // handle default case
        break;
}

You've noted in one of your responses that casting SelectedValue to string or int or 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 SelectedIndex only 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.

1 Comment

Also, if the combo box's Sorted property is true, adding new items could screw up the indexing.
2

SelectedValue is an object. cast it to an int in the switch.

3 Comments

I'm sorry, I'm new at coding. job_selecter is a combobox within Visual Studio. It contains a bunch of strings.
@Ben - I don't think there is any reason to be sorry; it's only easy when you know the answer.
i put (int) in front as you said, but it gave me another error when I compiled: nullreferenceexception was unhandled
1

You might have meant to use "SelectedIndex" property (a zero based number corresponding to your selection in combo OR a -1 when nothing is selected):

switch (job_selecter.SelectedIndex)
{
    case 0:
        head_seal_label.Text = "Ravager's Seal: Head (8)";
        break;
    // other cases for other Indices
    case -1:
    default:
        // handle nothing selected...
} 

Comments

0

You should get your SelectedIndex into an int first, to deal with this 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":

int index;
if(!Int32.TryParse(job_selector.SelectedIndex.ToString(), out index))
{
    index = -1;
}
//All your other cases here    
switch(index)
{
    case 0:
        head_seal_label.Text = "Ravager's Seal: Head (8)";
        break;

    default:
        head_seal_label.Text = "Some default Value";
        break;
}

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.