Sean Heiss
Sean Heiss

Reputation: 780

How do I return this variable using if statements?

I know this might be a silly question, with an easy answer, but after an hour of searching the internet I could not find a way to do this;

    public bool GetCollision(int x, int y)
    {
        bool isPassable;

        if (x < 0 || x >= 20)
        {
            isPassable = false;
        }

        if (y < 0 || y >= 20)
        {
            isPassable = true;
        }

        return isPassable;
    }

On the second-to-last line it says that isPassable is unassigned... yet clearly I assign to it in the if statements. There must be some fundamental misunderstanding of "if" statements on my part.

So, how can I do this? Thank you very much.

Upvotes: 5

Views: 6297

Answers (7)

looper
looper

Reputation: 1979

You should've a default-value:

bool isPassable = false;

If you can't exactly say, whether it's true or false, use nullable values instead, so isPassable can be null:

public bool? GetCollision(int x, int y)
{
    bool? isPassable = null;

    if (x < 0 || x >= 20)
    {
        isPassable = false;
    }

    if (y < 0 || y >= 20)
    {
        isPassable = true;
    }

    return isPassable;
}

Upvotes: 0

Pranav
Pranav

Reputation: 8871

"isPassable is Unassigned" Compiler is complaining because what if neither IF condition is satisfied . So you need to assign a value to it while declaring it like:-

 isPassable=false;

Upvotes: 2

Tim Kathete Stadler
Tim Kathete Stadler

Reputation: 1069

You could also do it this way:

public bool GetCollision(int x, int y)
{
    bool isPassable = false;

    if (y < 0 || y >= 20)
    {
        isPassable = true;
    }

    return isPassable;
}

This way, you can make sure that it returns true only when this condition is met, otherwise it returns just false.

Upvotes: 0

dutzu
dutzu

Reputation: 3910

That is because it doesn't have a default value set explicitly. Set isPassable to False by default and you're done.


Also you can do something like this:

return (!(x < 0 || x >= 20) && (y < 0 || y >= 20))

EDIT: The above solution would only work if an AND relationship would exist between your IFs.

Upvotes: 9

Mr Lister
Mr Lister

Reputation: 46559

The problem is that there are conditions under which the variable is not assigned at all. For instance, if x and y are both 0, the routine will return an unassigned variable. That is a no-no.

To clarify, the problem is not that there is no initial assigment (like isPassable = false;) but that the compiler warns you that you may have forgotten to check some conditions.

A construct like

bool isPassable;
if (...)
    isPassable = true;
else
    isPassable = false;
return isPassable;

would have been OK!

Upvotes: 2

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137398

What is the result of this function if x == 10 and y == 10? You have not defined what that result would be, and that is why the compiler is complaining. In this example, the first if statement would evaluate to false, and nothing would happen. Then the second if statement would evaluate to false, and the function would try to return with no value assigned to isPassable.

Upvotes: 4

Anujith
Anujith

Reputation: 9370

If none of If conditions evaluate to True, the variable will have nothing to return.

So, assign a default value to isPassable.

Upvotes: 3

Related Questions