Delegates
-
Hi, I believe i understand the concept of delegates. To put it simply, I think they are used to invoke methods in another class. What I don't quite understand is: What is the point of using delegates when you can easily call the method in the other class the normal way which is create an iinstance of the class and then call the method. Can you please help to understand this issue? Thanks
-
Hi, I believe i understand the concept of delegates. To put it simply, I think they are used to invoke methods in another class. What I don't quite understand is: What is the point of using delegates when you can easily call the method in the other class the normal way which is create an iinstance of the class and then call the method. Can you please help to understand this issue? Thanks
fmardani wrote:
What is the point of using delegates when you can easily call the method in the other class the normal way which is create an iinstance of the class and then call the method.
Because a delegate creates a link between classes where there often isn't one. For example, I create a form, form1. It has a modeless child, form2. I want to call a method in form1 when something happens in form2. Now, I CAN make a reference to form1 inside form2, but that's kind of ugly. Delegates are a nicer solution. Events can also be chained, so calling one event can call methods in several class instances. That's all happening for you, for free. Every time I go back to C++, delegates are the first thing I miss. Yes, I have function pointers, but it's a lot more work.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
fmardani wrote:
What is the point of using delegates when you can easily call the method in the other class the normal way which is create an iinstance of the class and then call the method.
Because a delegate creates a link between classes where there often isn't one. For example, I create a form, form1. It has a modeless child, form2. I want to call a method in form1 when something happens in form2. Now, I CAN make a reference to form1 inside form2, but that's kind of ugly. Delegates are a nicer solution. Events can also be chained, so calling one event can call methods in several class instances. That's all happening for you, for free. Every time I go back to C++, delegates are the first thing I miss. Yes, I have function pointers, but it's a lot more work.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Hi, I believe i understand the concept of delegates. To put it simply, I think they are used to invoke methods in another class. What I don't quite understand is: What is the point of using delegates when you can easily call the method in the other class the normal way which is create an iinstance of the class and then call the method. Can you please help to understand this issue? Thanks
In oop its an usual practice for an object to send a message to another object, itq common for an object to report back to the sender, resulting in two way conversation between objects (callback methods). C, C++ did this using function pointers. C# does it using delegates. Its a special type of object which contains details about the method rather than the data. They are used for two purposes 1. Callback 2. Event handling i hope this helps ---------------------------------------------------------------
where there is a will there is a way