Optional (or null) output parameters

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

    #1

    Optional (or null) output parameters

    I'm looking for a way to simulate the following kind of C code:

    --- C++ ---
    void F( double * array, double * fMean, double * fMedian, double *
    fMax, double * fMin );

    --- C# ---
    void F( double[] array, out double fMean, out double fMedian, out
    double fMax, out double fMin );

    I've read some of the examples on using overrides to simulate default
    values, but that's not exactly what I'm doing here. On the client
    side, NULL would be specified to indicate that (for example) mean and
    median aren't needed, but max and min are.

    Now, I can certainly use use overloads to have 2^4 -1 versions of this
    function. This would provide a maximally efficient way to calculate
    all these statistics; however, I don't need that, and the code
    duplication would kill me. Furthermore, the function above is just an
    example of the concept, the real code is more complex, of course.

    I could also pass a boolean next to each out parameter indicating
    whether to calculate it or not.

    I could also pass a structure, etc...

    I was kinda hoping I could pass in null for one of those output
    parameters, but then I realized there wouldn't be any way to check on
    the other side.

    I could also declare all the parameters as objects, pass in nulls for
    the ones I don't want, unbox the ones which were requested.

    Any other options? Which one of the above makes the most sense?

    Many Thanks,
    -ken
  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: Optional (or null) output parameters

    Ken,[color=blue]
    > I could also declare all the parameters as objects, pass in nulls for
    > the ones I don't want, unbox the ones which were requested.[/color]

    I would consider using a single Parameter Object. Either an actual object
    with multiple fields (Mean, Median, Max, Min, WantMean, WantMedian, WantMax,
    WantMin) or an extra enum flags parameter that indicates which values I am
    interested in.

    Something like:
    [Flags]
    enum StatsWanted
    {
    Mean = 1,
    Median = 2,
    Max = 4,
    Min = 8
    };

    void F( double[] array, StatsWanted statsWanted,
    out double fMean, out double fMedian,
    out double fMax, out double fMin );

    Then when I called it I would include the appropriate bits for what I was
    interested in.

    Hope this helps
    Jay

    "Ken Durden" <creepiecrawlie s@hotmail.com> wrote in message
    news:18c6b284.0 309060610.23e1b d8f@posting.goo gle.com...[color=blue]
    > I'm looking for a way to simulate the following kind of C code:
    >
    > --- C++ ---
    > void F( double * array, double * fMean, double * fMedian, double *
    > fMax, double * fMin );
    >
    > --- C# ---
    > void F( double[] array, out double fMean, out double fMedian, out
    > double fMax, out double fMin );
    >
    > I've read some of the examples on using overrides to simulate default
    > values, but that's not exactly what I'm doing here. On the client
    > side, NULL would be specified to indicate that (for example) mean and
    > median aren't needed, but max and min are.
    >
    > Now, I can certainly use use overloads to have 2^4 -1 versions of this
    > function. This would provide a maximally efficient way to calculate
    > all these statistics; however, I don't need that, and the code
    > duplication would kill me. Furthermore, the function above is just an
    > example of the concept, the real code is more complex, of course.
    >
    > I could also pass a boolean next to each out parameter indicating
    > whether to calculate it or not.
    >
    > I could also pass a structure, etc...
    >
    > I was kinda hoping I could pass in null for one of those output
    > parameters, but then I realized there wouldn't be any way to check on
    > the other side.
    >
    > I could also declare all the parameters as objects, pass in nulls for
    > the ones I don't want, unbox the ones which were requested.
    >
    > Any other options? Which one of the above makes the most sense?
    >
    > Many Thanks,
    > -ken[/color]


    Comment

    • Sunny

      #3
      Re: Optional (or null) output parameters

      Hi,
      you do not have to use out or ref if all your arguments are reference type.
      In C# all reference types are passed by reference by default.
      So in the caller you have to declare some objects and assign them values.
      For some of the objects you may assign null.
      And in your code you may check for null, change the values, etc.
      And no overolads :)

      I mean:

      void caller()
      {
      int[] res, par1, par2, par3, par4;

      par1 = new int[10];
      // fill values
      par2 = new int[5];
      par3 = null;
      par4 = null;
      res = null;

      myfunc(res, par1, par2, par, par4);
      if (res != null)
      //display results
      {}
      }

      void myfunc(int[] r, int[] a1, int[] a2, int[] a3, int[] a4)
      {
      if ((a1 != null) && (a2 != null))
      {
      r = new int[15];
      // do calcs
      }
      else
      {
      r = new int[10];
      //other calcs, or check for a3 and a4, etc...
      }
      }

      home that helps.
      Sunny

      P.S. watch out for typos, I've write directly in the reader, haven't tested,
      but it shoul work that way.

      "Ken Durden" <creepiecrawlie s@hotmail.com> wrote in message
      news:18c6b284.0 309060610.23e1b d8f@posting.goo gle.com...[color=blue]
      > I'm looking for a way to simulate the following kind of C code:
      >
      > --- C++ ---
      > void F( double * array, double * fMean, double * fMedian, double *
      > fMax, double * fMin );
      >
      > --- C# ---
      > void F( double[] array, out double fMean, out double fMedian, out
      > double fMax, out double fMin );
      >
      > I've read some of the examples on using overrides to simulate default
      > values, but that's not exactly what I'm doing here. On the client
      > side, NULL would be specified to indicate that (for example) mean and
      > median aren't needed, but max and min are.
      >
      > Now, I can certainly use use overloads to have 2^4 -1 versions of this
      > function. This would provide a maximally efficient way to calculate
      > all these statistics; however, I don't need that, and the code
      > duplication would kill me. Furthermore, the function above is just an
      > example of the concept, the real code is more complex, of course.
      >
      > I could also pass a boolean next to each out parameter indicating
      > whether to calculate it or not.
      >
      > I could also pass a structure, etc...
      >
      > I was kinda hoping I could pass in null for one of those output
      > parameters, but then I realized there wouldn't be any way to check on
      > the other side.
      >
      > I could also declare all the parameters as objects, pass in nulls for
      > the ones I don't want, unbox the ones which were requested.
      >
      > Any other options? Which one of the above makes the most sense?
      >
      > Many Thanks,
      > -ken[/color]

      Comment

      • Jasper Kent

        #4
        Re: Optional (or null) output parameters

        Unfortunately, fundamental types like double are value types and hence
        cannot be null. There are several ways round it.

        1) Pass the doubles as objects. An object can be null, or you can pass a
        double which will be boxed into the object. On the downside this is messy
        and not type safe.

        2) Create a class (hence a reference type) that encapsulates a double (a bit
        like the Java class Double). This will give you type safety, and the ability
        to pass a null object.

        Those are the most direct translations of the C++. Personally I'd return a
        structure and pass flags to say what needs to be calculated.

        Regards,

        Jasper Kent.
        "Ken Durden" <creepiecrawlie s@hotmail.com> wrote in message
        news:18c6b284.0 309060610.23e1b d8f@posting.goo gle.com...[color=blue]
        > I'm looking for a way to simulate the following kind of C code:
        >
        > --- C++ ---
        > void F( double * array, double * fMean, double * fMedian, double *
        > fMax, double * fMin );
        >
        > --- C# ---
        > void F( double[] array, out double fMean, out double fMedian, out
        > double fMax, out double fMin );
        >
        > I've read some of the examples on using overrides to simulate default
        > values, but that's not exactly what I'm doing here. On the client
        > side, NULL would be specified to indicate that (for example) mean and
        > median aren't needed, but max and min are.
        >
        > Now, I can certainly use use overloads to have 2^4 -1 versions of this
        > function. This would provide a maximally efficient way to calculate
        > all these statistics; however, I don't need that, and the code
        > duplication would kill me. Furthermore, the function above is just an
        > example of the concept, the real code is more complex, of course.
        >
        > I could also pass a boolean next to each out parameter indicating
        > whether to calculate it or not.
        >
        > I could also pass a structure, etc...
        >
        > I was kinda hoping I could pass in null for one of those output
        > parameters, but then I realized there wouldn't be any way to check on
        > the other side.
        >
        > I could also declare all the parameters as objects, pass in nulls for
        > the ones I don't want, unbox the ones which were requested.
        >
        > Any other options? Which one of the above makes the most sense?
        >
        > Many Thanks,
        > -ken[/color]


        Comment

        • Jon Skeet

          #5
          Re: Optional (or null) output parameters

          Sunny <sunnyask@icebe rgwireless.com> wrote:[color=blue]
          > you do not have to use out or ref if all your arguments are
          > reference type.[/color]

          You do sometimes.
          [color=blue]
          > In C# all reference types are passed by reference by default.[/color]

          No they're not. There's a difference between passing a reference by
          value and passing a value by reference. (Or a reference by reference.)

          Calling the default C# reference type parameter passing mechanism "pass
          by reference" is just wrong, and can be a source of confusion.

          See http://www.pobox.com/~skeet/csharp/parameters.html

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          Working...