Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method with a bool return

I was making a method with a bool return value and I had a problem:

This works

private bool CheckAll()
{
  //Do stuff
  return true;
}

But this dosn't, the method can't detect a return value if it's in a IF-statement.

private bool CheckAll()
{
  if (...)
  {
    return true;
  }
}

How can I fix this?

like image 903
Parys Bernard Avatar asked Nov 28 '22 05:11

Parys Bernard


1 Answers

private bool CheckAll()
{
    if ( ....)
    {
        return true;
    }

    return false;
}

When the if-condition is false the method doesn't know what value should be returned (you probably get an error like "not all paths return a value").

As CQQL pointed out if you mean to return true when your if-condition is true you could have simply written:

private bool CheckAll()
{
    return (your_condition);
}

If you have side effects, and you want to handle them before you return, the first (long) version would be required.

like image 141
Nadir Sampaoli Avatar answered Dec 11 '22 00:12

Nadir Sampaoli