Eg:
If a function looks like: int fact(int)
Then its function pointer should look like: int *p (int)
Here, as you may see the return types and the list of arguments of both function and function pointer match exactly.
When we use delegate then there are 3 stages which we need to follow:
Declaration: delegate
- )
Object Creation: delegateName objDelegate = new delegateName(
Invoking: objDelegate(
- )
The main uses of delegate comes in the following circumstances:
1) When we want to create the controls dynamically, then we cannot create the events for these controls beforehand as these controls are created only on runtime. In that case, if we want to bind these controls with their respective events then we need to use delegate.
So, delegate is first and foremost used for dynamic binding of controls with their events.
2) Delegate is also used in multitasking using threading.
Eg: Suppose we are copying a file from C: drive to D: drive. Then a progress bar would run which would indicate from time to time that how much contents have been copied - like 20% copied, 30% copied. This thing is an example of multiswitching, since here the thread is switching between 2 seperate opertaions. Once it is copying the data from one location to another location and then it is evaluating that how much content has been copied and increasing the progress bar accordingly. But, this switching is happening so fast, that the user gets a feeling as if 2 operations are happenng simultaneously.
So, in the above case, we may use a thread and make it switch between 2 operations. In this case also the delegate comes into play.
A detailed video tutorial has been provided on Delegate's usage for dynamic binding of controls with their events at the below link:
http://visiontechno.net/studymats/delegates.html
Sam HobbsPosted Nov 11, 2010, 1:37 PM
Akhil GuptaPosted Nov 11, 2010, 12:59 PM
Sam HobbsPosted Nov 10, 2010, 4:29 PM
When I tried the link a second time, I did not get the security check thing and I did get a YouTube video but a separate browser window also opened and there was a totally different video in the other page and the audio I heard was from the other page, not the video with Visual Studio so that makes the video useless.
Manikavelu VelayuthamPosted Nov 10, 2010, 2:15 AM
Manikavelu VelayuthamPosted Nov 10, 2010, 2:14 AM
Both the circumstances were really a good example to know the appropriate usage of delegates.
When I was a beginner, I was searching in google to know the exact usage of delegates. But only definitions are available everywhere and not clearly explained.
But you post would clear all the doubts on delegates.
Thanks Akhil