goku9384 goku9384 - 1 year ago 56
C# Question

C# Method not all code paths return a value

I'm creating the following method that returns a normalized decimal value. The AngleUnits is an enum that contains Degrees, Gradians, Radians, and Turns. However after implementing this code I get "Angle.Normalize(...) not all code paths return a value". Not sure what I'm missing here as I am returning the decimal value. Thanks in advance.

private static decimal Normalize(decimal value, AngleUnits units)
{
decimal normalizedValue;

switch (units)
{
case AngleUnits.Degrees:
if (value >= 0 && value <= 360)
{
normalizedValue = value;
return normalizedValue;
}
else if (value < 0)
{
value = value + 360;
normalizedValue = value;
return normalizedValue;
}
else if (value > 360)
{
value = value - 360;
normalizedValue = value;
return normalizedValue;
}
break;

case AngleUnits.Gradians:
if (value >= 0 && value <= 400)
{
normalizedValue = value;
return normalizedValue;
}
else if (value < 0)
{
value = value + 400;
normalizedValue = value;
return normalizedValue;
}
else if (value > 400)
{
value = value - 400;
normalizedValue = value;
return normalizedValue;
}
break;

case AngleUnits.Radians:
if (value >= 0 && value <= twoPi)
{
normalizedValue = value;
return normalizedValue;
}
else if (value < 0)
{
value = value + twoPi;
normalizedValue = value;
return normalizedValue;
}
else if (value > twoPi)
{
value = value - twoPi;
normalizedValue = value;
return normalizedValue;
}
break;

case AngleUnits.Turns:
if (value >= 0 && value <= 1)
{
normalizedValue = value;
return normalizedValue;
}
else if (value < 0)
{
value = value + 1;
normalizedValue = value;
return normalizedValue;
}
else if (value > 1)
{
value = value - 1;
normalizedValue = value;
return normalizedValue;
}
break;
}
}

Answer Source

You have got this error because you can have a call to this method with an angleunits that has no match in the switch statement and your method should return a method So the compiler is detecting this.

when you close your switch statement

you should return a default value

or throw an exception

Throw new Exception("no units was found  ")

here a snippet of your method

  private static decimal Normalize(decimal value, AngleUnits units)
        {
            decimal normalizedValue;

            switch (units)
            {
                case AngleUnits.Degrees:
                    if (value >= 0 && value <= 360)
                    {
                        normalizedValue = value;
                        return normalizedValue;
                    }
                    else if (value < 0)
                    {
                        value = value + 360;
                        normalizedValue = value;
                        return normalizedValue;
                    }
                    else if (value > 360)
                    {
                        value = value - 360;
                        normalizedValue = value;
                        return normalizedValue;
                    }
                    break;

                default: throw new Exception("no Angleunits match was found");  






            }
            return value; 


        }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download