Guang
Guang

Reputation: 61

How do I solve this error "not all code paths return a value"

The setSpeedX was underlined for the error "not all code paths return a value". May I know how do I solve it? The codes are as below:

class Ball
{
    public int speedX { get; private set; }
    public int speedY { get; private set; }
    public int positionX { get; private set; }
    public int positionY { get; private set; }

    public Ball(int speedX, int speedY, int positionX, int positionY)
    {
        this.speedX = speedX;
        this.speedY = speedY;
        this.positionX = positionX;
        this.positionY = positionY;
    }

    public int setSpeedX(int newSpeedX)
    {
        speedX = newSpeedX;
    }    

    public int setSpeedY(int newSpeedY)
    {
        speedY = newSpeedY;
    }

    public int setPositionX(int newPositionX)
    {
        positionX = newPositionX;
    }

    public int setPositionY(int newPositionY)
    {
        positionY = newPositionY;
    }
}

Thank you.

Upvotes: 0

Views: 4218

Answers (4)

Kyle
Kyle

Reputation: 1

Either return a value

public int setSpeedX(int newSpeedX)
{
    speedX = newSpeedX;
    return(speedX);
}

Or change the method to void

public void setSpeedX(int newSpeedX)
{
    speedX = newSpeedX;
}

There doesn't seem to be much value in returning a value

Upvotes: 0

Martin Mulder
Martin Mulder

Reputation: 12954

You are setting a value in your methods (setSpeedX, setSpeedY, setPositionX, setPositionY ), but not returning anything. But the signature of the methods have a return type int.

So... replace the return type int with void, like this:

public void setSpeedX(int newSpeedX)
{
    speedX = newSpeedX;
}    

public void setSpeedY(int newSpeedY)
{
    speedY = newSpeedY;
}

public void setPositionX(int newPositionX)
{
    positionX = newPositionX;
}

public void setPositionY(int newPositionY)
{
    positionY = newPositionY;
}

or return a value of type int, like this:

public int setSpeedX(int newSpeedX)
{
    speedX = newSpeedX;
    return speedX;
}    

public int setSpeedY(int newSpeedY)
{
    speedY = newSpeedY;
    return speedY;
}

public int setPositionX(int newPositionX)
{
    positionX = newPositionX;
    return positionX;
}

public int setPositionY(int newPositionY)
{
    positionY = newPositionY;
    return positionY;
}

Upvotes: 0

gzaxx
gzaxx

Reputation: 17590

Add return to your methods that should return value like:

public int setPositionY(int newPositionY)
{
    positionY = newPositionY;
    return positionY;
}

or change them to return void:

public void setPositionY(int newPositionY)
{
    positionY = newPositionY;
}

Upvotes: 3

Bart Friederichs
Bart Friederichs

Reputation: 33511

You never put a return statement, so no value is returned, even though you declare the method that it should.

There are two ways to fix this:

make the method void:

public void setSpeedX(int newSpeedX)
{
    speedX = newSpeedX;
}

or return a value:

public int setSpeedX(int newSpeedX)
{
    speedX = newSpeedX;
    return speedX;
}

This goes for all methods by the way, not just setSpeedX.

Upvotes: 1

Related Questions