user2883073
user2883073

Reputation:

what do they mean with "not all code paths return a value"?

--> Error 'Minesweeper.Game.Gewonnen()': not all code paths return a value.

What am I doin' wrong?

public bool Gewonnen()
{

  for (int y = 0; y < _boolArray.GetLength(0); y++)
  {
    for (int x = 0; x < _boolArray.GetLength(1); x++)
    {
      if (_boolArray[x, y] == false)
        return false;

      else
        return true;
    }
  }
}

Upvotes: 2

Views: 153

Answers (3)

nico008
nico008

Reputation: 111

gzaxx solution will compile. Anyway, I'm not sure that it is what you want because in that case it will always return after the first iteration (if any). Here is another try (I hope it helps):

public bool Gewonnen()
{

  for (int y = 0; y < _boolArray.GetLength(0); y++)
  {
    for (int x = 0; x < _boolArray.GetLength(1); x++)
    {
      if (_boolArray[x, y] == false)
        return false;
    }
  }

  return true;  //this mean _boolArray has no false value
}

Upvotes: 1

Voidpaw
Voidpaw

Reputation: 920

Let's assume for example that your first for-loop would at some point be something like:

for (int y = 0; y < _boolArray.GetLength(0); y++) { }

and at that point in time your _boolArray has a length of 0. It would then result in

for (int y = 0; y < 0; y++) { }

That means it is instantly done with this piece of code, going to the end of the loop. But wait: this function was supposed to return a boolean value, and there is no return whatsoever.

That in a nutshell is what "not all code paths return a value" means. You are not allowed to have any (unlikely?) combination of values that would make you end the function without returning the a value of the type mentoin in the function (boolean in this case)

Upvotes: 0

gzaxx
gzaxx

Reputation: 17600

That means not all possible execution paths returns value. In you example loop may not be executed even once (when _boolArray length is 0) and if that happens your method would not return anything. Add return false at the end of method:

public bool Gewonnen() 
{
    for (int y = 0; y < _boolArray.GetLength(0); y++)
    {
         for (int x = 0; x < _boolArray.GetLength(1); x++)
         {
              if (_boolArray[x, y] == false)
                  return false;
              else
                  return true;
         }
    }

    return false;
}

Upvotes: 8

Related Questions