-2

I want to make a combobox with numbers 1 to 10.

private void cbKiesTafel_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    int i = 1;
    while (i <= 10)
    {
        string KiesTafel = ((ComboBoxItem)cbKiesTafel.SelectedItem).Content.ToString();

        lbTafels.Items.Add(BepaalTafel(KiesTafel));
        i++;
     }
}

private string BepaalTafel(string KiesTafel) (the problem is here)
{
    int i = 1;
    while (i <= 10)
        i++;
    switch (KiesTafel)
    {
        case "1":
            return (lbTafels.Items.Add(i + " x " + KiesTafel + " = " + i*1).ToString());
        case "2":
            return (lbTafels.Items.Add(i + " x " + KiesTafel + " = " + i*2).ToString());
        case "3":
            return (lbTafels.Items.Add(i + " x " + KiesTafel + " = " + i*3).ToString());
        case "4":
            return (lbTafels.Items.Add(i + " x " + KiesTafel + " = " + i*4).ToString());
        case "5":
            return (lbTafels.Items.Add(i + " x " + KiesTafel + " = " + i*5).ToString());
        case "6":
            return (lbTafels.Items.Add(i + " x " + KiesTafel + " = " + i*6).ToString());
        case "7":
            return (lbTafels.Items.Add(i + " x " + KiesTafel + " = " + i*7).ToString());
        case "8":
            return (lbTafels.Items.Add(i + " x " + KiesTafel + " = " + i*8).ToString());
        case "9":
            return (lbTafels.Items.Add(i + " x " + KiesTafel + " = " + i*9).ToString());
        case "10":
            return (lbTafels.Items.Add(i + " x " + KiesTafel + " = " + i*10).ToString());
    }
}
Izzy
  • 6,740
  • 7
  • 40
  • 84
olivier
  • 5
  • 2
  • 1
    You need `default` section in your `switch` statement. – Salah Akbari Nov 18 '15 at 10:12
  • 1
    Just a side note, but your BepaalTafel method looks weird: What's point of incrementing `i` inside a `while` loop to 10? Your `switch` could be replaced by just int.TryParse(KiesTafel) and a range check (greater-or-equal 1 and smaller-or-equal 10). – Filburt Nov 18 '15 at 10:24
  • I tried to answer but I'm not 100% sure if it gives the desired result. Could you explain how the selected `KiesTafel` value influences the `Tafels` items? – Filburt Nov 18 '15 at 11:29

5 Answers5

2

Looks like you need a default option:

switch (KiesTafel)
{
    case "1":
        return (lbTafels.Items.Add(i + " x " + KiesTafel + " = " + i*1).ToString());
    case "2":
        return (lbTafels.Items.Add(i + " x " + KiesTafel + " = " + i*2).ToString());
    case "3":
        return (lbTafels.Items.Add(i + " x " + KiesTafel + " = " + i*3).ToString());
    case "4":
        return (lbTafels.Items.Add(i + " x " + KiesTafel + " = " + i*4).ToString());
    case "5":
        return (lbTafels.Items.Add(i + " x " + KiesTafel + " = " + i*5).ToString());
    case "6":
        return (lbTafels.Items.Add(i + " x " + KiesTafel + " = " + i*6).ToString());
    case "7":
        return (lbTafels.Items.Add(i + " x " + KiesTafel + " = " + i*7).ToString());
    case "8":
        return (lbTafels.Items.Add(i + " x " + KiesTafel + " = " + i*8).ToString());
    case "9":
        return (lbTafels.Items.Add(i + " x " + KiesTafel + " = " + i*9).ToString());
    case "10":
        return (lbTafels.Items.Add(i + " x " + KiesTafel + " = " + i*10).ToString());

    default:
        return "";
}

Unless you place a default in there, the compiler will think Hold on, if KiesTafel is none of the case values, then what am I supposed to return?

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
0

Your case statement is not returning a value on all inputs, hence your method isn't either so naturally the compiler will complain.

Add a default (it's hit when switch doesn't match any other case) to your switch and return something.

switch (KiesTafel)
{
    case 1: ...
    case 2: ...
    ...
    default:
        return String.Empty;   
}
kkyr
  • 3,785
  • 3
  • 29
  • 59
0

If your value is neither of your described value (from 1 to 10) then it will not return. Thus you get an error saying not all code paths return value.

You can fix that by adding default keyword to the end of your switch.

default:
    return String.Empty;

Another solution could be return something if non of the values are meet. Just at the end of your method write return String.Empty;

Karolis Kajenas
  • 1,523
  • 1
  • 15
  • 23
0

Add a default case with a return to your switch, or add a return after the switch.

anthonytimmers
  • 258
  • 1
  • 8
0

While all other answers correctly address your issue at hand I would like to point out some flaws in your overall approach:

Your method BepaalTafel tries to access a global object which can cause you a lot of trouble and is generally considered bad practice.

Your method returns a string while it already adds to the ComboBox items which is at best redundant.

Instead I'd suggest to use a far simpler way of generating the desired items:

private void cbKiesTafel_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    string KiesTafel = ((ComboBoxItem)cbKiesTafel.SelectedItem).Content.ToString();

    int kies = 0;
    int.TryParse(KiesTafel, out kies);
    var tafels = Enumerable.Range(1, 10).Select(i => string.Format("{0} x {1} = {2}", kies, i, i * kies));
    lbTafels.ItemSource = tafels;
}
Filburt
  • 17,626
  • 12
  • 64
  • 115
  • When i use this method i have always the 10. How can i have a better solution when i click in the combobox. – olivier Nov 19 '15 at 08:41
  • When i click on the combobox, the answer should be for example: the 5, 1*5= 5 , 2* 5 = 10, ... – olivier Nov 19 '15 at 08:42
  • I modified my example to factor in the selected value from `KiesTafel`. This should give you the desired list of items. – Filburt Nov 19 '15 at 08:59