473,326 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

VB.NET Delegates


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
4 1367
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jeffrey T. Smith | last post by:
Back when the new J2SE1.5 features were announced, there was a JavaLive community chat (http://java.sun.com/developer/community/chat/JavaLive/2003/jl0729.html) in which Neal Gafter explains the...
3
by: Sam | last post by:
I’m just starting to learn delegates. I’m at the very beginning. If I understand correctly, delegates are for when you want to pass a function as a parameter. For example the client provides a...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
4
by: AMDRIT | last post by:
I am trying to understand Delegates and where/when to use them. I can see one potential use of a delegate (on form closing, set the cancel property in the event arguments.) Does anyone have a...
6
by: =?Utf-8?B?Sko=?= | last post by:
I have a logger component that logs to multiple sources, ie textfile, eventlog etc. and I have two methods that depending on where I call up my logger comp. one of them will be called. For ex. if...
0
by: bharathreddy | last post by:
Delegates Here in this article I will explain about delegates in brief. Some important points about delegates. This article is meant to only those who already know delegates, it will be a quick...
6
by: =?Utf-8?B?T2xkQ2FEb2c=?= | last post by:
My question is regarding the use of delegates in C#. I see how .Net uses delegates to wire event handlers to events. It’s an object created by a single line of code by the system and that makes...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
69
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same...
9
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.