DreamKid Posted November 5, 2005 Posted November 5, 2005 I do understand how delegates works. I just want to know why use it? What's it's purpose? Thank you. Quote
Leaders snarfblam Posted November 5, 2005 Leaders Posted November 5, 2005 There are all different uses. You can use a delegate for a callback function. Suppose you need to call a function which varies based on a certain condition. You can check the condition each time you need to call the function... If MyString = "Error" Then ProcessError() ElseIf MyString = "Success" Then ProcessSuccess() ElseIf MyString = "Inconclusive" Then Conclude() ' ... this could go on and on End If Instead, as when the condition changes, point a delegate to the appropriate function... [Vb] Public Sub SetSuccess() MyFunction = AddressOf Success() End Sub 'And when we need to call the appropriate function, we only need a single line of code... MyFunction() [/code] This could be used for optimization for speed. It could be used to organize or simplify code. There are lots of other uses for delegates. I'll bet some other people could post some other basic uses, but you could also get creative. You could create a history feature (as in that of Photoshop) that maintains a list, in the form of an array of delegates (and an array of their parameters, if necessary), of functions needed to undo/redo actions. Quote [sIGPIC]e[/sIGPIC]
DreamKid Posted November 5, 2005 Author Posted November 5, 2005 There are all different uses. You can use a delegate for a callback function. Suppose you need to call a function which varies based on a certain condition. You can check the condition each time you need to call the function... If MyString = "Error" Then ProcessError() ElseIf MyString = "Success" Then ProcessSuccess() ElseIf MyString = "Inconclusive" Then Conclude() ' ... this could go on and on End If Instead, as when the condition changes, point a delegate to the appropriate function... [Vb] Public Sub SetSuccess() MyFunction = AddressOf Success() End Sub 'And when we need to call the appropriate function, we only need a single line of code... MyFunction() [/code] True.. but I'll still need to set the condition if it's success or not? Like when would SetSuccess() run? Quote
Leaders snarfblam Posted November 5, 2005 Leaders Posted November 5, 2005 True.. but I'll still need to set the condition if it's success or not? Like when would SetSuccess() run? Let's forget about SetSuccess(). Just as an example, suppose you perform an operation and during that operation catch an exception. You would set the delegate to point to the ProcessError() function. If you reach the end of the process, you point the delegate at ProcessSuccess(). Again, this is just an example of a possible application of delegates, and not even a great one at that. A great example would be the event system in .Net. Down at the CLR level, events are not actually a unique entity. An event is just a variable of type MulticastDelegate, which inherits from Delegate and adds the ability to hold multiple function pointers for the sake of multiple handlers. When you use code like this: Public Event MyEvent '... AddHandler Me.MyEvent, AddressOf MyFunction '... RaiseEvent MyEvent() it is essentially compiled as something similar to 'Our multicast delegate is stored in this variable Public MyEvent As System.Delegate '... 'Here we add a new callback to our multicast delegate via Delegate.Combine MyEvent = System.Delegate.Combine(MyEvent, AddressOf MyFunction) '... 'Raise the event MyEvent.Invoke() 'Call callback function [/Vb] In other words, the entire event system in .Net relies on callbacks, which rely on delegates. And now you are thanking your lucky stars that we have delegates. Quote [sIGPIC]e[/sIGPIC]
DreamKid Posted November 6, 2005 Author Posted November 6, 2005 Thanks. That really helps alot. :) Am not quite understand what MulticastDeligate yet, but sure get the ideas there.. :D Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.