I have enum list and method and i get error: ” not all code paths return a value“
Some idea whats wrong in my method ? I am sure I always return STANY type :/
Thanks for help 🙂
private enum STANY { PATROL, CHAT, EAT, SEARCH, DIE };
private STANY giveState(int id, List<Ludek> gracze, List<int> plansza)
{
// Sprawdz czy gracz stoi na polu z jedzeniem i nie ma 2000 jednostek jedzenia
bool onTheFood = false;
onTheFood = CzyPoleZjedzeniem(id, gracze, plansza, onTheFood);
if (onTheFood && (gracze[id].IloscJedzenia < startFood / 2))
return STANY.EAT;
// Sprawdz czy gracz nie stoi na polu z innym graczem
bool allKnowledge = true;
allKnowledge = CzyPoleZInnymGraczem(id, gracze, allKnowledge);
if (!allKnowledge)
return STANY.CHAT;
// Jesli ma ponad i rowna ilosc jedzenia patroluj
if (gracze[id].IloscJedzenia >= startFood / 2)
return STANY.PATROL;
// Jesli ma mniej niz polowe jedzenia szukaj jedzenia
if (gracze[id].IloscJedzenia > 0 && gracze[id].IloscJedzenia < startFood / 2)
return STANY.SEARCH;
// Jesli nie ma jedzenia umieraj
if (gracze[id].IloscJedzenia <= 0)
return STANY.DIE;
}
Maybe you’re sure that a return type will always be given, but the compiler isn’t [imagine that all the if conditions fail – ie: a variable was changed while your code was executing by an external program. Then what would happen?]
Just stick a return at the bottom as a “default” value. You could also throw an exception too, if you want, since the bottom should never be reached, right?