By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,572 Members | 1,516 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,572 IT Pros & Developers. It's quick & easy.

VB.NET Delegates

P: n/a

I'm really not sure when I might have to use one.

can any one give me an example of when they should be used?
--
http://www.douglassdavis.com

Nov 8 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi Douglas!

Mostly they are used to implement events behaviours. For example, we
have a timer instance and we want that for every tick, one procedure is
invoked. The easiest way is to declare one delegate for this timer
associated to the procedure. Something like this...

Dim Timer1 As Timer
Dim timerDelegate As System.Threading.TimerCallback = AddressOf
MyProcedure
Dim autoEvent As New AutoResetEvent(False)
Timer1 = New Timer(timerDelegate, autoEvent, 500, 250)

Private Sub MyProcedure(ByVal sender As System.Object)

'Code that you want to be executed in every tick

End Sub

End Class

douglass_da...@earthlink.net ha escrito:
I'm really not sure when I might have to use one.

can any one give me an example of when they should be used?
--
http://www.douglassdavis.com
Nov 8 '06 #2

P: n/a
They also come in very handy for creating multithreaded solutions and a
couple other neat things. Here's some links to a 4 part series from
msdn's Basic Instincts columns that explains some of the features of
delegates.

http://msdn.microsoft.com/msdnmag/is...asicInstincts/

http://msdn.microsoft.com/msdnmag/is...asicInstincts/

http://msdn.microsoft.com/msdnmag/is...asicInstincts/

http://msdn.microsoft.com/msdnmag/is...s/default.aspx

That should explain their uses better than I could. Let us know if you
need any clarifications on anything you read.

Thanks,

Seth Rowe

Nov 8 '06 #3

P: n/a
I use them to assign event handlers.

AddHandler SaveButton.Click, AddressOf MySaveRoutine
AddHandler MenuStripSaveButton.Click, AddressOf MySaveRoutine

This way, I can have one save routine that is not attached
to the buttons except by the events raised. Makes it
easier to maintain, and I can call the save routine
from anywhere else I want.

Here's another example:

Dim deleg as DisplayMessage
deleg = New DisplayMessage(AddressOf WriteToDebugWindow)

where OutputInformation looks like this:
Sub WriteToDebugWindow(ByVal msgText as String)
Debug.WriteLine(msgText)
End Sub

Then I can use this all over the place to invoke my
WriteToDebugWindow routine:

deleg.Invoke("You have an error here, the user pressed F2.")
deleg.Invoke("The user put in a date of 1888.")
deleg.Invoke("The user needs more training; " & _
"he keeps banging his head against the monitor.")

What if I want to change these to write to a flat file?

I can write a routine and call it, say WriteToFlatFile.
Then I can change the declaration of the delegate to this:

deleg = New DisplayMessage(AddressOf WriteToFlagFile).

And voila! All my messages get written out to a file
instead of printed on the debug line.

Francesco Balena has a keen example of using delegates
to do callback methods to print out the names of folders
inside folders, and use it to search for folders, etc.,
in his book "Programming Microsoft Visual Basic 2005:
The Language". This is the first place I read about
using delegates that really explained why I would
want to.

Robin S.

<do************@earthlink.netwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
>
I'm really not sure when I might have to use one.

can any one give me an example of when they should be used?
--
http://www.douglassdavis.com

Nov 8 '06 #4

P: n/a
Douglas,

You only have to use them if there is no other solution, and because the VB
team is trying to make as much possible without delegates, that is seldom.

However, I see a lot of misusing delegates here probably because the one who
uses it thinks it makes him interesting or things like that.

Cor

<do************@earthlink.netschreef in bericht
news:11**********************@m7g2000cwm.googlegro ups.com...
>
I'm really not sure when I might have to use one.

can any one give me an example of when they should be used?
--
http://www.douglassdavis.com

Nov 9 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.