Click to See Complete Forum and Search --> : When would you use delegates??


jcw
April 21st, 2002, 06:15 PM
Hi. I've read a lot about delegates in VB.NET and I understand the mechanics of creating them. What I don't get is what situations you would use them in or what advantages they bring. Why would it be better to pass a delegate from classA to ClassB rather than just instantiating ClassA from ClassB and using ClassA's methods that way? Thanks.

vchapran
May 8th, 2002, 03:58 PM
It's just to make our life harder.
Had the same question when read about delegates.
Vlad

Iouri
May 10th, 2002, 02:34 PM
I think that the only good thing about it that a delegate can refer to multiple sub/functions. That means that delegate can hold a list of functions that are invoked when you invoke the delegate


Iouri Boutchkine
iouri@hotsheet.com

Cakkie
May 14th, 2002, 02:47 AM
Delegates are there for the same reason as we had interfaces in VB6. They ensure that whatever funtion is reference, it will have the signature we reuire, meaning that we can know at designtime what parameters we can pass, although we don't know to what function we will be passing them eventually.

Take multithreading for instance, we need to pass a delegate for ThreadStart, which is a procedure that doesn't take any parameters. Because we need to pass a delegate, it needs to be a procedure with no parameters, the name doesn't matter. Passing any other procedure (with parameters) will result in error.

Tom Cannaerts
slisse@planetinternet.be.remove.this.bit
Moderator on http://www.vbcodelibrary.co.uk/board

A bottomless pit, I'm sure it came with the place, who would dig one on purpose?