The compiler is showing "not all code paths return a value" for the CalculateCharges. Help!!

public decimal CalculateCharges()
        {
            decimal pay;
            decimal decHoursParked;
            decHoursParked = Math.Ceiling(_hoursParked);
            if (decHoursParked <= 3)
            {
                pay = 4;
            }
            else if (decHoursParked > 3)
            {
                pay = decHoursParked * 2 - 2;
            }
            if (decHoursParked >= 24)
            {
                pay = 12;
            }
}

Recommended Answers

All 4 Replies

In line 1 you declare that you are going to return a decimal. Where is the return statement in your code?

so I need a

return pay;

at the end?

Yes!
Only a void method does not need a return statement.

Also, anywhere your function may exit, you need a return.

For example, consider:

bool GetError(bool error) {
if (error == true) {
    return true;
}
}

That snippet will throw the same error. What you need is

bool GetError(bool error) {
if (error == true) {
    return true;
}
return false;
}

Now in that example, it was pretty obvious. Just be careful if/when your methods start to get really intricate and convoluted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.