Method returns value error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joseph

    Method returns value error

    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 retFinalPercent age. Please
    look at the code below and offer your advice. Thanks

    public static double SelectFinalPerc ent2(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 retFinalPercent age;

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

    return retFinalPercent age; //<-- Use of unassigned local variable
    retFinalPercent age
    }
  • Jon Skeet [C# MVP]

    #2
    Re: Method returns value error

    Joseph <Joseph@discuss ions.microsoft. comwrote:
    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 retFinalPercent age. Please
    look at the code below and offer your advice. Thanks
    >
    public static double SelectFinalPerc ent2(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 retFinalPercent age;
    >
    for(int x = 0; x < aAge.Length; ++x)
    {
    if(imyFuture == aAge[x])
    {
    retFinalPercent age = aPercent[x];
    x = aAge.Length;
    }
    }
    >
    return retFinalPercent age; //<-- Use of unassigned local variable
    retFinalPercent age
    }
    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
    retFinalPercent age 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.

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • Joseph

      #3
      Re: Method returns value error

      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 [C# MVP]" wrote:
      Joseph <Joseph@discuss ions.microsoft. comwrote:
      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 retFinalPercent age. Please
      look at the code below and offer your advice. Thanks

      public static double SelectFinalPerc ent2(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 retFinalPercent age;

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

      return retFinalPercent age; //<-- Use of unassigned local variable
      retFinalPercent age
      }
      >
      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
      retFinalPercent age 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.
      >
      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too
      >

      Comment

      • Joseph

        #4
        Re: Method returns value error

        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" wrote:
        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 [C# MVP]" wrote:
        >
        Joseph <Joseph@discuss ions.microsoft. comwrote:
        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 retFinalPercent age. Please
        look at the code below and offer your advice. Thanks
        >
        public static double SelectFinalPerc ent2(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 retFinalPercent age;
        >
        for(int x = 0; x < aAge.Length; ++x)
        {
        if(imyFuture == aAge[x])
        {
        retFinalPercent age = aPercent[x];
        x = aAge.Length;
        }
        }
        >
        return retFinalPercent age; //<-- Use of unassigned local variable
        retFinalPercent age
        }
        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
        retFinalPercent age 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.

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        • Bruce Wood

          #5
          Re: Method returns value error

          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 retFinalPercent age 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 SelectFinalPerc ent2(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 ArgumentExcepti on(String.Forma t("{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.

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Method returns value error

            Joseph <Joseph@discuss ions.microsoft. comwrote:
            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 SelectFinalPerc ent2(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 ArgumentExcepti on ("Invalid index passed");
            }

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

            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too

            Comment

            • Joseph

              #7
              Re: Method returns value error

              Thanks guys, your replies were very instructive and helpful.

              "Jon Skeet [C# MVP]" wrote:
              Joseph <Joseph@discuss ions.microsoft. comwrote:
              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 SelectFinalPerc ent2(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 ArgumentExcepti on ("Invalid index passed");
              }
              >
              It makes it very clear what will happen if you pass the wrong thing in,
              an exception will be thrown.
              >
              --
              Jon Skeet - <skeet@pobox.co m>
              http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
              If replying to the group, please do not mail me too
              >

              Comment

              Working...