Delegates
-
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.
-
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.
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.
-
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.
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.
-
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.
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."
-
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.
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
-
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."
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.
-
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.