Please Explain me about Delegates

chandru

New member
Joined
Mar 12, 2007
Messages
3
Location
india
Programming Experience
1-3
Hi Friends,
I want to know about delegates. Can you please explain
with an example in vb.net.
Thanks in advance
chandru
 
I'm not a pro, by here's my explanation (hope it helps):
How to you call another function without knowing its name? Just like you receive a variable ByRef without its name, you can do that with subs & functions. They are "safe" function pointers, or delegates.

If you want to create a delegate for a function, do this:
VB.NET:
Public Delegate Sub MyDelegate(ByVal arg1 As Integer)
and receive such a delegate by:
VB.NET:
Public Sub MySub(ByVal del As MyDelegate)

If someone calls MySub, they should do this:
VB.NET:
MySub(New MyDelegate(AddressOf AnotherSubWithTheRightArguments))


Hope this helps, and please correct any problems.
 
Delegates are also used by events. When an event is declared VB.Net creates a hidden delegate by same signature. It's the invocation list of this delegate that all subscribed instance methods are added to or removed from through WithEvents/AddHandler/RemoveHandler, and all these methods are called (invoked) in succession when the event is raised.
 
Hi Friends, Thank you very much for your support.The approach is simple and at the same time effective to learn about delegates.Thanking you all for the help. have a great dayRegardschandru
 
Back
Top