Re: Method returns value error
- From: "Bruce Wood" <brucewood@xxxxxxxxxx>
- Date: 8 Nov 2006 16:27:09 -0800
Perhaps, but the _compiler_ doesn't know that. It looks at the method
in isolation. It sees that there is a path through the method for which
the retFinalPercentage variable doesn't get set, so it complains.
It's always good to put error handling in your code, even in those
"this can never happen" case. The impossible happens once in a while,
trust me.
So, if the caller can't possibly pass a value not in the array, that
leaves you with something like this:
public static double SelectFinalPercent2(int imyFuture)
{
int[] aAge = {55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65};
double[] aPercent = {.37, .40, .43, .49, .55, .61, .67, .73, .82,
..91, 1.0};
for(int x = 0; x < aAge.Length; ++x)
{
if(imyFuture == aAge[x])
{
return aPercent[x];
}
}
throw new ArgumentException(String.Format("{0} is not a valid
age.", imyFuture), "imyFuture");
}
Joseph wrote:
A better reply to your question is that the user is only limiter to the ones
listed in int[] aAge. The passing parameter will always be ones listed. I
have the necessary error providers in place to insure that.
.
- References:
- Re: Method returns value error
- From: Jon Skeet [C# MVP]
- Re: Method returns value error
- Prev by Date: How to make a c# regex match only the *last* occurrence of a pattern?
- Next by Date: help me plzzz in c# some Q
- Previous by thread: Re: Method returns value error
- Next by thread: Re: Method returns value error
- Index(es):