netmajor
netmajor

Reputation: 6585

" not all code paths return a value" when return enum type

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;
    }

Upvotes: 0

Views: 440

Answers (6)

egrunin
egrunin

Reputation: 25053

Replace this:

// Jesli nie ma jedzenia umieraj
if (gracze[id].IloscJedzenia <= 0)
    return STANY.DIE;

With this:

// Jesli nie ma jedzenia umieraj
// (gracze[id].IloscJedzenia <= 0)
return STANY.DIE;

Leave the redundant line in the comment as documentation.

Upvotes: 3

dlras2
dlras2

Reputation: 8486

This is because every return statement is preceded by an if statement. Therefore, if all of your if statements are false, no value will be returned. If it's impossible for all if statements to be false, remove if (gracze[id].IloscJedzenia <= 0), as it's redundant. If not, add a return null; or something at the end, or throw an error.

Upvotes: 3

Dean Harding
Dean Harding

Reputation: 72658

The compiler's static analysis (i.e. the code that is designed to check for this condition) is not always 100% accurate. But when it's not 100% accurate, the compiler will tend to err on the side of giving a false positive (that is, saying it doesn't return from all paths) than a false negative (that is, not saying it doesn't return from all paths).

So add a return at the bottom, use a series of "if .. else if ... else" statements instead, or throw an exception to indicate that the condition is "impossible".

Upvotes: 1

derek
derek

Reputation: 4886

there's no return if none of those if conditions are met. You need to either use if...elseif...else or have a return after all of the if statements that will return a value if nothing has been returned (none of the if conditions were met).

Upvotes: 8

EricLaw
EricLaw

Reputation: 57085

This has nothing to do with the fact that you're returning an ENUM, and everything to do with the fact that the compiler detects that there are cases where you never call RETURN with any value.

You should add a return statement at the bottom of your function that returns some default value.

Upvotes: 3

Warty
Warty

Reputation: 7395

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?

Upvotes: 4

Related Questions