|
-
Dec 2nd, 2002, 09:00 AM
#1
Thread Starter
Addicted Member
General question about delegates?
Hi
Can anyone here please explain to me what is delegates? and in what situation should we use delegates?
I have try looking for answer around the web but i cant seem to find a satisfactory explanation
Thanks
-
Dec 2nd, 2002, 10:09 AM
#2
Registered User
A delegate is a type-safe function pointer. Its commonly used to pass a reference to the entry point for a method and invoke that method without making an explicit method call. You the declare it something like this:
Code:
Public Delegate Function TestDelegate (ByVal value As Single) As Single
If you then create a function similar to the following.....
Code:
Public Function Sqrt(ByVal value As Single) As Single
Return value * value
End Function
You can now declare the delegate something like this....
Code:
Dim aDelegate As New TestDelegate(adressof Sqrt)
Now you can call this delegate to invoke the method....
This is often used with events, to create a delegate to point to a specifik event that you through code want to raise. In C#¤ you must explicitly designate the delegate type that your event will use. VB.NET does alot of this behind the scenes, and the use of delegates for event is not actually neede.
But I still hope you get a notion of what a delegate is.... even if I can't find a really good example where to use it.
-
Dec 2nd, 2002, 10:23 AM
#3
Lively Member
A good example of when you might want to use delegates is in a client/server situation where you have a client that continuously need updated information from a server. For example to update the list of users online.
Instead of having all the clients continuously asking the server for a list of online users, the server just keeps one copy of the online users and sends that same list to all the clients that has subscribed to it (by passing in a delegate) a predefined intervals (that can be changed based on server load for example).
The server simply loops through the delegate collection that it keeps to call each individual client one at a time to update all of them with the same list of online users.
Taking this one step further is to spin a separate thread in the server for each client to invoke the delegate. This way you wouldn't run into a problem whereby each client is waiting for the server to finish calling the delegate of another client. As each delegate call is a separate thread, each is operating autonomously and even if one of the clients goes down and the thread dies, the other threads keep on running.
-
Dec 2nd, 2002, 10:41 AM
#4
Thread Starter
Addicted Member
Thanks for all the explanation
but there is something i still dont understand, whats the difference if i declare a delegated and point to a function compare with calling the function directly?, doesnt both accomplish the same task?
Thanks
-
Dec 2nd, 2002, 10:49 AM
#5
Registered User
More or less.... But lets say you have a button that you want to press to invoke a specific function depending on parameters of the program.
Example (not that very good maybe, but what the heck)
Say you have radiobuttons to determine what function to use for a value.... SquareRoot(value), Squared(value), 1/value and so on..... you might have a delegate invoke a funktion on the button click, and set a new adress for the delegate on the radiobutton clicks to rerute to another function.
The server example from m1k3l is alot better, but maybe this helps you to find better use.......
-
Dec 3rd, 2002, 10:53 AM
#6
Lively Member
When directly calling functions, you need to know exactly what is the function name beforehand.
One way to get around this is to use interfaces but with interfaces and different implementations, you still have some restrictions because, the other object need to be implementing a specific interface in order for that object to be able to call it.
Using delegates is another way.
Another example (unorthodox but good IMHO) is in a situation like this:
You have a set of routines that you need repeat everytime, but in the middle is some code that will be different for different objects. For example, routine to attach things to the CallContext when using remoting to instantiate and call remoting objects at a server. This routine is redundant in a sense that it's repeated over and over again no matter what object I will be calling in the remoting server.
So one way is to code it again and again everytime i want to call a remoting server object to do something.
Another way is to use delegates.
Using delegate, I can pass in a delegate (of the function that will actually be calling the remoting object to perform my task) to another function in a separate general object.
What this function will do is to first do the redundant routines that are repetitive and THEN it will use the delegate to perform the actual task.
Taking that another step is to include other stuffs into the general object function for things like recovery/auto retry if a connection cannot be establish, generalized error handling, and many more stuffs like that.
If you are having problem understanding this, try to think of it as a burger/sandwich
The general function object thingy is the bread and the delegate is the meat sandwiched in between. The bread is always standardized but the meat can be anything at all.
The possibilities are endless, and using delegates this way (i would call it a sandwich pattern ; ) makes the system easily extensible without the limitations of using interfaces.
And future programmers can go on programming without needing to know the intricaices of the bread on top or below but just concentrate on the meat.
Delegates are one of the coolest things that happened to the VB world IMO. Explore them, try them out, they're worth the time needed to learn how to use them.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|