Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Delegates

Delegates

Scheduled Pinned Locked Moved C#
questioncom
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DELETEUSER
    wrote on last edited by
    #1

    I have been reading from various sources about delegates. The more I read the more I get confused. My question is - What can you do with delegates that you cannot do with just calling the method and not using delegate(s) howsoever? I mean assume that when an event occurs then instead of having a delegate call the method just call the method directly from the event handler. Also if you can send any good link that explains the concept it will be appreciated. Thanks ---------- Venus Patel http://patelsinc.blogspot.com/ A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.

    R G D 3 Replies Last reply
    0
    • D DELETEUSER

      I have been reading from various sources about delegates. The more I read the more I get confused. My question is - What can you do with delegates that you cannot do with just calling the method and not using delegate(s) howsoever? I mean assume that when an event occurs then instead of having a delegate call the method just call the method directly from the event handler. Also if you can send any good link that explains the concept it will be appreciated. Thanks ---------- Venus Patel http://patelsinc.blogspot.com/ A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.

      R Offline
      R Offline
      Roy Heil
      wrote on last edited by
      #2

      I am sure there are other advantages, but the one that comes to mind is this: Say you create a control dynamically. Since it is not created at design time, you have no other way to associate your sub with your control. What you need to do is create your delegate, and when you create your control at runtime, associate the delegate with the controls method call. Hope this helps. Roy.

      1 Reply Last reply
      0
      • D DELETEUSER

        I have been reading from various sources about delegates. The more I read the more I get confused. My question is - What can you do with delegates that you cannot do with just calling the method and not using delegate(s) howsoever? I mean assume that when an event occurs then instead of having a delegate call the method just call the method directly from the event handler. Also if you can send any good link that explains the concept it will be appreciated. Thanks ---------- Venus Patel http://patelsinc.blogspot.com/ A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.

        G Offline
        G Offline
        Gerben Jongerius
        wrote on last edited by
        #3

        Why are delegates used instead of just calling the function in the event handler. There are plenty of reasons. Note that delegates are used to bind functions to events, or to call other objects and functions async. Lets first explain the first, because of delegates you can add more then one listener to the event. This is usefull in multiview application. The second one is, at least for me, the most important reason. Thanks to the delegate you will be ably to dynamicly invoke a method. This means that a call to the function will be made, but not executed straight away. So the function calling the delegate continues executing its code.

        D 1 Reply Last reply
        0
        • G Gerben Jongerius

          Why are delegates used instead of just calling the function in the event handler. There are plenty of reasons. Note that delegates are used to bind functions to events, or to call other objects and functions async. Lets first explain the first, because of delegates you can add more then one listener to the event. This is usefull in multiview application. The second one is, at least for me, the most important reason. Thanks to the delegate you will be ably to dynamicly invoke a method. This means that a call to the function will be made, but not executed straight away. So the function calling the delegate continues executing its code.

          D Offline
          D Offline
          Den2Fly
          wrote on last edited by
          #4

          Gerben I think your second reason is wrong while the calling method whether directly or using its delegate run in the same thread as caller function, it doesn't continue to next statement until the called method(s) return. --- "Art happens when you least expect it."

          G 1 Reply Last reply
          0
          • D DELETEUSER

            I have been reading from various sources about delegates. The more I read the more I get confused. My question is - What can you do with delegates that you cannot do with just calling the method and not using delegate(s) howsoever? I mean assume that when an event occurs then instead of having a delegate call the method just call the method directly from the event handler. Also if you can send any good link that explains the concept it will be appreciated. Thanks ---------- Venus Patel http://patelsinc.blogspot.com/ A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.

            D Offline
            D Offline
            Den2Fly
            wrote on last edited by
            #5

            Let me add some notes: 1. A Delegate is a type that is used to construct delegate objects that refrence functions with the same signature as delegate. Just like classes that construct objects. 2. When a delegate object is created with some function name in its constructor, that object will be indirect agent of that function, and this happens RUNTIME. It is important because when you create a compiled class for your users you don't know what functions they will create to bind as their callbacks or event handlers so you call them directly, so there should be some indirect way for them to introduce their functions and that is Delegate! I hope that is useful - Mohammad --- "Art happens when you least expect it." -- modified at 1:06 Tuesday 13th December, 2005

            1 Reply Last reply
            0
            • D Den2Fly

              Gerben I think your second reason is wrong while the calling method whether directly or using its delegate run in the same thread as caller function, it doesn't continue to next statement until the called method(s) return. --- "Art happens when you least expect it."

              G Offline
              G Offline
              Gerben Jongerius
              wrote on last edited by
              #6

              If you invoke a methond dynamicly, even if they operate in the same thread, the running function will not get interrupted. This is because using dynamic invoke you post a message to your application with the request to execute a function. You do not call the function directly. Also see: MSDN Article on dynamic invokes. PS: I tried it in a single thread application just to be sure.

              D 1 Reply Last reply
              0
              • G Gerben Jongerius

                If you invoke a methond dynamicly, even if they operate in the same thread, the running function will not get interrupted. This is because using dynamic invoke you post a message to your application with the request to execute a function. You do not call the function directly. Also see: MSDN Article on dynamic invokes. PS: I tried it in a single thread application just to be sure.

                D Offline
                D Offline
                Den2Fly
                wrote on last edited by
                #7

                Yes, I see. So it is one other usage of delegates. Thank you --- "Art happens when you least expect it."

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups