Jump to content
Xtreme .Net Talk

Recommended Posts

  • Leaders
Posted

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.

[sIGPIC]e[/sIGPIC]
Posted
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?

  • Leaders
Posted
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.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...