|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
VB.NET DelegatesI'm really not sure when I might have to use one.
can any one give me an example of when they should be used? 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: Show quoteHide quote > 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 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/issues/02/12/BasicInstincts/ http://msdn.microsoft.com/msdnmag/issues/03/01/BasicInstincts/ http://msdn.microsoft.com/msdnmag/issues/04/01/BasicInstincts/ http://msdn.microsoft.com/msdnmag/issues/04/05/BasicInstincts/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 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. <douglass_da***@earthlink.net> wrote in message Show quoteHide quote news:1162981405.719028.148730@m7g2000cwm.googlegroups.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 > 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 <douglass_da***@earthlink.net> schreef in bericht Show quoteHide quote news:1162981405.719028.148730@m7g2000cwm.googlegroups.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 >
DirctX 9.0 SDK And Visual Basic
collection object not showing properties of stored object? Put If e.Cancel Then Exit Sub in Closing event COMM Class Need help in developing an editor in VB.net RadioButtonList asp.net "the object is currently in use elsewhere" Clickable map? KNOW THE SIZE OF A FIELD richtext in a datagrid? |
|||||||||||||||||||||||