Don't understand delegates

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

    Don't understand delegates

    I've read every example i could find on the subject and still couldn't
    figure out its proper usage.

    What's the point of delegates, why can't I just invoke the method
    directly???

    Can someone please help?

    Tem

  • =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=

    #2
    RE: Don't understand delegates

    Delegates are pointers to methods. They are a way of having a method as a
    variable which can be passed around. They are handy for dependency injection
    and things. For example. I have some classes which initialise my business
    layer at the start of my application. They take a Delegate to a method like
    void ReportStatus(st ring)
    The function call this delegate passing a string which states what they are
    currently doing for initialisation. This allows be to change how they report
    their information by passing in different methods.
    The windows forms app passes in a reference to a method which updates a
    label on the splash screen. The command line version passes in a method with
    prints to the command line. and potentially a server version could pass one
    in which logs to a file/database etc and if i dont want to log at all i can
    pass a null delegate
    The good thing about this is that the logic for logging is seperated
    entirely from the logic for initialisation so they are changeable and
    testable independantly.

    Does this help you see the usefullness of them?
    --
    Ciaran O''Donnell
    try{ Life(); } catch (TooDifficultException) { throw Toys(); }



    "Tem" wrote:
    I've read every example i could find on the subject and still couldn't
    figure out its proper usage.
    >
    What's the point of delegates, why can't I just invoke the method
    directly???
    >
    Can someone please help?
    >
    Tem
    >
    >

    Comment

    • =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=

      #3
      RE: Don't understand delegates

      This is effectively how events work. In a normal situation like the below,
      you could use an event which is effectively a list of delegates to call when
      the event is raised. There is a reason in the below example why it isnt an
      event but its not important for this discussion.


      --
      Ciaran O''Donnell
      try{ Life(); } catch (TooDifficultException) { throw Toys(); }



      "Ciaran O''Donnell" wrote:
      Delegates are pointers to methods. They are a way of having a method as a
      variable which can be passed around. They are handy for dependency injection
      and things. For example. I have some classes which initialise my business
      layer at the start of my application. They take a Delegate to a method like
      void ReportStatus(st ring)
      The function call this delegate passing a string which states what they are
      currently doing for initialisation. This allows be to change how they report
      their information by passing in different methods.
      The windows forms app passes in a reference to a method which updates a
      label on the splash screen. The command line version passes in a method with
      prints to the command line. and potentially a server version could pass one
      in which logs to a file/database etc and if i dont want to log at all i can
      pass a null delegate
      The good thing about this is that the logic for logging is seperated
      entirely from the logic for initialisation so they are changeable and
      testable independantly.
      >
      Does this help you see the usefullness of them?
      --
      Ciaran O''Donnell
      try{ Life(); } catch (TooDifficultException) { throw Toys(); }

      >
      >
      "Tem" wrote:
      >
      I've read every example i could find on the subject and still couldn't
      figure out its proper usage.

      What's the point of delegates, why can't I just invoke the method
      directly???

      Can someone please help?

      Tem

      Comment

      • Ignacio Machin ( .NET/ C# MVP )

        #4
        Re: Don't understand delegates

        On Apr 25, 2:48 am, "Tem" <tem1...@yahoo. comwrote:
        I've read every example i could find on the subject and still couldn't
        figure out its proper usage.
        >
        What's the point of delegates, why can't I just invoke the method
        directly???
        Because maybe you do not know which exact method to call. Or maybe the
        exact method to call change dynamically.

        One way of think of it as if your class is incomplete and is the code
        using it who is going to complete it.

        Comment

        • Cowboy \(Gregory A. Beamer\)

          #5
          Re: Don't understand delegates

          Ignacio points out the primary reason: You are not sure what you are calling
          until runtime.

          Delegates are aslo useful for event handlers, when you only want a specific
          bit of code called in a certain instance.

          Another reason to delegate is callbacks, which cannot be coded directly as
          method calls. I actually restated reason #2, only in different language, and
          reason #1, at least in some instances. :-)

          --
          Gregory A. Beamer
          MVP, MCP: +I, SE, SD, DBA

          Subscribe to my blog


          or just read it:


          *************** *************** *************** ****
          | Think outside the box!
          |
          *************** *************** *************** ****
          "Tem" <tem1232@yahoo. comwrote in message
          news:eRpQtBqpIH A.5096@TK2MSFTN GP02.phx.gbl...
          I've read every example i could find on the subject and still couldn't
          figure out its proper usage.
          >
          What's the point of delegates, why can't I just invoke the method
          directly???
          >
          Can someone please help?
          >
          Tem

          Comment

          • Peter Duniho

            #6
            Re: Don't understand delegates

            On Fri, 25 Apr 2008 08:09:18 -0700, Cowboy (Gregory A. Beamer)
            <NoSpamMgbworld @comcast.netNoS pamMwrote:
            Ignacio points out the primary reason: You are not sure what you are
            calling
            until runtime.
            Careful with the "until runtime". _Some_ code usually knows what's going
            to be called at compile time. It's just that the _calling_ code doesn't
            know until runtime.

            With that in mind, I'd say that technically speaking, all uses of
            delegates fall into this category. For example...
            Delegates are aslo useful for event handlers, when you only want a
            specific
            bit of code called in a certain instance.
            But the reason they are used for event handlers is that at compile time,
            the implementer of the event doesn't know what code will be called. The
            ..NET Forms classes are a classic example of this. Microsoft can't
            possibly know what your own code would be when they provide an event. So
            using a delegate allows other code to provide the reference to the method
            to be called, well after the point at which Microsoft's code was compiled.
            Another reason to delegate is callbacks, which cannot be coded directly
            as
            method calls.
            That's not strictly speaking correct, depending on what you mean by "as
            method calls". Java doesn't have any idea like delegates, and yet it can
            implement the same sort of behavior. It uses interfaces instead. And
            those are coded directly as method calls (i.e. a method defined in an
            interface implemented by whichever class wants to provide the callback).
            This could be done in .NET, and in fact for more elaborate APIs is in fact
            used quite a lot.

            But regardless, that's still a subset of the general "you don't know at
            compile time the exact method that will be called".

            I prefer delegates, and I feel that for single-method situations they are
            way more convenient than having to create a whole interface and then
            implement it in each class that wants to provide a method to call at
            specific times (whether to support events, i/o callbacks, whatever). But
            you don't _have_ to have delegates to allow for callbacks that aren't
            known at compile time.

            Pete

            Comment

            • Jeff Louie

              #7
              Re: Don't understand delegates

              Tem... You can use delegates when you need "runtime polymorphic"
              behavior.

              Runtime polymorphism involves programming to a type, such that the
              implementation details can be different and are discoverable at runtime.
              When
              programming to a type, the caller does not know the class of the object,
              only
              that the object implements the type of interest.

              Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!

              Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


              Regards,
              Jeff

              *** Sent via Developersdex http://www.developersdex.com ***

              Comment

              Working...