Question

I'm trying to create a method for my web-service however i receive this error listed in the title

Use of unassigned local variable 'totalroadtax'

This is how the user have to input 2 different variable to trigger the method in the webmethod

[WebMethod]
public double RoadTax(int engineCapacity, int vehicleAge)
{
    double totalroadtax;

    if (engineCapacity.Equals("600") && vehicleAge.Equals("12"))
    {
        totalroadtax = ((200.00 * 0.782) * (1.10));
    }

    return totalroadtax;
    //return (engineCapacity - vehicleAge);
}

I declared my totalroadtax in my method, input some caluclation method and return the value. I checked the have the necessary information for this method to work but I still receive the error. Did i leave anything out here?

Was it helpful?

Solution

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.

OTHER TIPS

totalroadtax is unassigned when if condition is not met. Change your declaration to:

double totalroadtax = 0d;

It's good practice to always initialize variables.

In this particular case if the if evaluates to false, the variable totalroadtax will be returned uninitialized. To fix the issue you need to initialize totalroadtax:

double totalroadtax = 0d;

Here is an alternative nicer solution imo, avoiding the variable all together:

[WebMethod]
public double RoadTax(int engineCapacity, int vehicleAge)
{
    if (engineCapacity.Equals("600") && vehicleAge.Equals("12"))
    {
        return ((200.00 * 0.782) * (1.10));
    }

    return 0d;
}

There's not much to add to previous answers so I'll start off with the obvious: your method does not initialize the variable.

You also mentioned your method also returns 0 every time. Consider this line:

engineCapacity.Equals("600") && vehicleAge.Equals("12")

Here's why:

In this case, Equals will compare a string ("600") and an int (engineCapacity and vehicleAge). The if condition will fail and return 0. Compare the values using the == equality operator using ints, instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top