fabulous Level: VB Guru

 Registered: 03-08-2002 Posts: 439
|
Re: what is delegate and what is their use.
In .NET, you are using delegates almost all the time when you respond to events. A delegate is simply a piece of code that will execute on behalf of another. If you ever used Pascal, you would know that you could have function variables where you could define a variable as a function. The actual function implementation would/could be written somewhere else, and by someone else, and then assigned to this variable. The original code would call the variable and any function assigned to it would be executed.
A delegate is a similar thing, you can define your own for many reasons, and you decide on the signature that you want for the delegate. You basically do it this way
| Public Delegate Sub DoSomething() |
Classes that implement your delegate can call it what they will as long as the signature is the same, i.e. - no return type and no arguments. (Don't talk back and don't argue). I use delegates to execute certain code asychronously, such as building my user interface while loading info from the database:
Dim lb As New LoadBoothsDelegate(AddressOf LoadBooths)
Dim aresult As IAsyncResult = BeginInvoke(lb) |
The BeginInvoke allows a method to be executed on the same thread as the calling routine but allows the rest of normal processing to go on without waiting for the code to be finished first. Delegates are useful in this area. Some people actually use them in place of events but that is another story altogether. Happy coding 
____________________________
My boss is a Jewish Carpenter (Jesus Christ)

Brain Bench Certified VB.NET Developer
|