Extention method return types

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?=

    Extention method return types

    I'm do meore and more with extention methods, totally cool. They are making
    life much easier for IU developers. I'm trying to add a return type of struct
    to a asp.net DropDownList of type struct. The idea is that if the conversion
    to say Int32 failed and a default value was used i can also return a
    Pass/Fail bool, int code to let the developer know that it failed or the
    reason for the failure.

    Where should the struse be defined for the return type? It's a simple
    question, but i really don't know the answer.

    Has anyone else done this and do you know and artical or have an example.

    thanks!!!
    --
    Share The Knowledge. I need all the help I can get and so do you!
  • Jon Skeet [C# MVP]

    #2
    Re: Extention method return types

    On Aug 1, 3:12 pm, Yankee Imperialist Dog
    <YankeeImperial ist...@discussi ons.microsoft.c omwrote:
    I'm do meore and more with extention methods, totally cool. They are making
    life much easier for IU developers. I'm trying to add a return type of struct
    to a asp.net DropDownList of type struct. The idea is that if the conversion
    to say Int32 failed and a default value was used i can also return a
    Pass/Fail bool, int code to let the developer know that it failed or the
    reason for the failure.
    >
    Where should the struse be defined for the return type? It's a simple
    question, but i really don't know the answer.
    >
    Has anyone else done this and do you know and artical or have an example.
    A few questions:
    1) Why do you want it to be a struct?
    2) Why do you want to use a code for the failure instead of a message?

    As for where the type is defined - anywhere, basically. An extension
    method is just a normal static method with some extra syntactic sugar,
    so the normal rules apply.

    Jon

    Comment

    • =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?=

      #3
      Re: Extention method return types

      I appreciate the very prompt reply!
      1. I would like two values returned, would a class be a better choice?
      1.a I can call two seprate extention methods for valueexists then ToInt32,
      but i may also want to retrun the reason like it's empty, it's null, it's
      just not there.

      2. Then in the class file that defines the methods for the name space would
      make sense?
      --
      Share The Knowledge. I need all the help I can get and so do you!


      "Jon Skeet [C# MVP]" wrote:
      On Aug 1, 3:12 pm, Yankee Imperialist Dog
      <YankeeImperial ist...@discussi ons.microsoft.c omwrote:
      I'm do meore and more with extention methods, totally cool. They are making
      life much easier for IU developers. I'm trying to add a return type of struct
      to a asp.net DropDownList of type struct. The idea is that if the conversion
      to say Int32 failed and a default value was used i can also return a
      Pass/Fail bool, int code to let the developer know that it failed or the
      reason for the failure.

      Where should the struse be defined for the return type? It's a simple
      question, but i really don't know the answer.

      Has anyone else done this and do you know and artical or have an example.
      >
      A few questions:
      1) Why do you want it to be a struct?
      2) Why do you want to use a code for the failure instead of a message?
      >
      As for where the type is defined - anywhere, basically. An extension
      method is just a normal static method with some extra syntactic sugar,
      so the normal rules apply.
      >
      Jon
      >

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Extention method return types

        Yankee Imperialist Dog <YankeeImperial istDog@discussi ons.microsoft.c om>
        wrote:
        I appreciate the very prompt reply!
        1. I would like two values returned, would a class be a better choice?
        Either would be okay, but class is a better "default choice". Unles
        you've got a reason to make it a value type, I'd stick with a class.
        1.a I can call two seprate extention methods for valueexists then ToInt32,
        but i may also want to retrun the reason like it's empty, it's null, it's
        just not there.
        Returning a reason is fine, but a string tends to be easier to
        understand than an integer.
        2. Then in the class file that defines the methods for the name space would
        make sense?
        No, the class which defines the extension methods has to be a static
        class, but you could do it in the same namespace.

        --
        Jon Skeet - <skeet@pobox.co m>
        Web site: http://www.pobox.com/~skeet
        Blog: http://www.msmvps.com/jon.skeet
        C# in Depth: http://csharpindepth.com

        Comment

        • =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?=

          #5
          Re: Extention method return types

          noted as for the text, i wanted to keep this example simple for an answer.
          Thanks you , you answered my question.
          Very appreciated
          --
          Share The Knowledge. I need all the help I can get and so do you!


          "Jon Skeet [C# MVP]" wrote:
          Yankee Imperialist Dog <YankeeImperial istDog@discussi ons.microsoft.c om>
          wrote:
          I appreciate the very prompt reply!
          1. I would like two values returned, would a class be a better choice?
          >
          Either would be okay, but class is a better "default choice". Unles
          you've got a reason to make it a value type, I'd stick with a class.
          >
          1.a I can call two seprate extention methods for valueexists then ToInt32,
          but i may also want to retrun the reason like it's empty, it's null, it's
          just not there.
          >
          Returning a reason is fine, but a string tends to be easier to
          understand than an integer.
          >
          2. Then in the class file that defines the methods for the name space would
          make sense?
          >
          No, the class which defines the extension methods has to be a static
          class, but you could do it in the same namespace.
          >
          --
          Jon Skeet - <skeet@pobox.co m>
          Web site: http://www.pobox.com/~skeet
          Blog: http://www.msmvps.com/jon.skeet
          C# in Depth: http://csharpindepth.com
          >

          Comment

          Working...