The if
may be skipped (if the condition is false) and you're returning totalroadtax
. The variable is not initialized at that point because you declared it without an initial value.
You should declare the variable with some initial value like:
double totalroadtax = 0;
Edit:
Your code needlessly compares int
values to string
literals. It's cleaner and more efficient to perform the condition this way:
[WebMethod]
public double RoadTax(int engineCapacity, int vehicleAge)
{
double totalroadtax = 0;
if (engineCapacity == 600 && vehicleAge ==12)
{
totalroadtax = ((200.00 * 0.782) * (1.10));
}
return totalroadtax;
}
There's no need to call Equals()
- the ==
operator reads better and it performs a type-safe check.
I'd also recommend that you don't perform direct equality checks against floating-point values. Instead of using ==
, you should really check for something like:
if ( (engineCapacity >599.9999 && engineCapacity < 600.00001) && ...
This is because floating-point values are not exact representations of fractional numbers.
Read this question for more details.