views:

120

answers:

6

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;
    }
+4  A: 

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?

ItzWarty
+3  A: 

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.

EricLaw -MSFT-
If you look at his last three conditions, though, they're technically mutually exclusive: *one* of them has to be true. I think it's just that the compiler's static analysis doesn't pick it up (not that I would expect it to, really...)
Dean Harding
It all depends on the value of id
RobS
@RobS: what? @codeka is correct, logically one of the last three conditions *must* be true.
egrunin
@egrunin and what happens if id is outside the bounds of the List?
RobS
@RobS: it will throw an exception at the first instance of `gracze[id].IloscJedzenia`. Doesn't affect @codeka's point.
egrunin
I agree, I'm just saying that it might be a rationale for the compiler error
RobS
+7  A: 

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).

derek
+1  A: 

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".

Dean Harding
+3  A: 

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.

Daniel Rasmussen
+3  A: 

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.

egrunin