I’m getting a compile time error
‘UserQuery.ReturnInt(UserQuery.Foo)’: not all code paths return a value
Unless I’m not seeing something in the code, the switch statement should return 0 as the default value so all code paths do return a value.
enum Foo
{
Bar,
Zoo,
Boo
}
void Main()
{
Foo test = Foo.Bar;
Console.WriteLine (ReturnInt(test));
}
int ReturnInt(Foo test) {
int someOtherValue = 4; // <---Value may change depending on X
switch (test)
{
case Foo.Bar:
if (someOtherValue > 20)
return 1;
break;
case Foo.Zoo:
if (someOtherValue == 5)
return 4;
break;
case Foo.Boo:
if (someOtherValue == 2)
return 7;
break;
default:
return 0;
}
}
Any of the other switch blocks will just hit the “break” statement depending on the value of “someOtherValue”. You have no return statement after the switch, so any of the situations where the “break” is hit will not return a value.