Method returns value error

G

Guest

Hi All,

I am trying to create a method that executes a parallel array and returns a
value. I am not sure why the method I wrote is generating an error. The error
is displayed as: Use of unassigned local variable retFinalPercentage. Please
look at the code below and offer your advice. Thanks

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};
double retFinalPercentage;

for(int x = 0; x < aAge.Length; ++x)
{
if(imyFuture == aAge[x])
{
retFinalPercentage = aPercent[x];
x = aAge.Length;
}
}

return retFinalPercentage; //<-- Use of unassigned local variable
retFinalPercentage
}
 
J

Jon Skeet [C# MVP]

Joseph said:
I am trying to create a method that executes a parallel array and returns a
value. I am not sure why the method I wrote is generating an error. The error
is displayed as: Use of unassigned local variable retFinalPercentage. Please
look at the code below and offer your advice. Thanks

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};
double retFinalPercentage;

for(int x = 0; x < aAge.Length; ++x)
{
if(imyFuture == aAge[x])
{
retFinalPercentage = aPercent[x];
x = aAge.Length;
}
}

return retFinalPercentage; //<-- Use of unassigned local variable
retFinalPercentage
}

What do you want the method to return if someone passes in a value
which isn't in the list given? There's nothing to guarantee that
retFinalPercentage will have been assigned a value before it's meant to
be returned, so what do you want it to do?

By the way, I know various people maintain that it's always better to
have a single point of return, but in this case I'd return from inside
the loop. The alternative is to use "break" - it's clearer than setting
the index to make this iteration the last.
 
G

Guest

I am wanting to pass an integer, one which is listed in int[] aAge. It then
finds the one parallel to it in double[] aPercent and passes that back to
some previous code.
For instance if 58 gets passed to the method, then .49 will get returned to
the caller.
Thanks

Jon Skeet said:
Joseph said:
I am trying to create a method that executes a parallel array and returns a
value. I am not sure why the method I wrote is generating an error. The error
is displayed as: Use of unassigned local variable retFinalPercentage. Please
look at the code below and offer your advice. Thanks

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};
double retFinalPercentage;

for(int x = 0; x < aAge.Length; ++x)
{
if(imyFuture == aAge[x])
{
retFinalPercentage = aPercent[x];
x = aAge.Length;
}
}

return retFinalPercentage; //<-- Use of unassigned local variable
retFinalPercentage
}

What do you want the method to return if someone passes in a value
which isn't in the list given? There's nothing to guarantee that
retFinalPercentage will have been assigned a value before it's meant to
be returned, so what do you want it to do?

By the way, I know various people maintain that it's always better to
have a single point of return, but in this case I'd return from inside
the loop. The alternative is to use "break" - it's clearer than setting
the index to make this iteration the last.
 
G

Guest

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.

Joseph said:
I am wanting to pass an integer, one which is listed in int[] aAge. It then
finds the one parallel to it in double[] aPercent and passes that back to
some previous code.
For instance if 58 gets passed to the method, then .49 will get returned to
the caller.
Thanks

Jon Skeet said:
Joseph said:
I am trying to create a method that executes a parallel array and returns a
value. I am not sure why the method I wrote is generating an error. The error
is displayed as: Use of unassigned local variable retFinalPercentage. Please
look at the code below and offer your advice. Thanks

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};
double retFinalPercentage;

for(int x = 0; x < aAge.Length; ++x)
{
if(imyFuture == aAge[x])
{
retFinalPercentage = aPercent[x];
x = aAge.Length;
}
}

return retFinalPercentage; //<-- Use of unassigned local variable
retFinalPercentage
}

What do you want the method to return if someone passes in a value
which isn't in the list given? There's nothing to guarantee that
retFinalPercentage will have been assigned a value before it's meant to
be returned, so what do you want it to do?

By the way, I know various people maintain that it's always better to
have a single point of return, but in this case I'd return from inside
the loop. The alternative is to use "break" - it's clearer than setting
the index to make this iteration the last.
 
B

Bruce Wood

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");
}
 
J

Jon Skeet [C# MVP]

Joseph said:
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.

And how is the compiler meant to know that? What should happen if your
logic is wrong, and an inappropriate value is passed in? Here's an
alternative version which takes that into account:

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 ("Invalid index passed");
}

It makes it very clear what will happen if you pass the wrong thing in,
an exception will be thrown.
 
G

Guest

Thanks guys, your replies were very instructive and helpful.

Jon Skeet said:
Joseph said:
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.

And how is the compiler meant to know that? What should happen if your
logic is wrong, and an inappropriate value is passed in? Here's an
alternative version which takes that into account:

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 ("Invalid index passed");
}

It makes it very clear what will happen if you pass the wrong thing in,
an exception will be thrown.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top