|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
ContentsIntroductionWhen I was trying to learn events and delegates, I read a lot of articles to completely understand what they are and how to use them, and now I want to present them all here, everything I learned, most of the things you need to learn. What are delegates?Delegate and Event concepts are completely tied together. Delegates are just function pointers, That is, they hold references to functions. A Every delegate has a signature. For example: Delegate int SomeDelegate(string s, bool b);
is a delegate declaration. When I say this delegate has a signature, I mean that it returns an I said, when you instantiate delegates, you pass in the function name to which this delegate will refer as its constructor parameter. The important thing to notice is that only functions that have the same signature as the delegate, can be passed as a parameter. Consider the following function: private int SomeFunction(string str, bool bln){...}
You can pass this function to SomeDelegate sd = new SomeDelegate(SomeFunction);
Now, sd("somestring", true);
Now that you know how to use delegates, let's understand events... Understanding Events
Want to understand what's happening? Let's learn through an example:
Our class has an event: public event NumberReachedEventHandler NumberReached;
In the above declaration, You see, before we declare our event, we need to define our delegate (our event handler). It could be something like this: public delegate void NumberReachedEventHandler(object sender,
NumberReachedEventArgs e);
As you see, our delegate's name is: Have you ever used public class NumberReachedEventArgs : EventArgs
{
private int _reached;
public NumberReachedEventArgs(int num)
{
this._reached = num;
}
public int ReachedNumber
{
get
{
return _reached;
}
}
}
If it wouldn't be necessary to provide the user with any information, we just use the Now, every thing is prepared to take a look inside our namespace Events
{
public delegate void NumberReachedEventHandler(object sender,
NumberReachedEventArgs e);
/// <summary>
/// Summary description for Counter.
/// </summary>
public class Counter
{
public event NumberReachedEventHandler NumberReached;
public Counter()
{
//
// TODO: Add constructor logic here
//
}
public void CountTo(int countTo, int reachableNum)
{
if(countTo < reachableNum)
throw new ArgumentException(
"reachableNum should be less than countTo");
for(int ctr=0;ctr<=countTo;ctr++)
{
if(ctr == reachableNum)
{
NumberReachedEventArgs e = new NumberReachedEventArgs(
reachableNum);
OnNumberReached(e);
return;//don't count any more
}
}
}
protected virtual void OnNumberReached(NumberReachedEventArgs e)
{
if(NumberReached != null)
{
NumberReached(this, e);//Raise the event
}
}
}
In the above code, we raise an event if we reach the desired number. There are a lot of things to consider here:
OK. Now, it's time to practically use our In our sample application, we have two textboxes named
And here is the event handler for private void cmdRun_Click(object sender, System.EventArgs e)
{
if(txtCountTo.Text == "" || txtReachable.Text=="")
return;
oCounter = new Counter();
oCounter.NumberReached += new NumberReachedEventHandler(
oCounter_NumberReached);
oCounter.CountTo(Convert.ToInt32(txtCountTo.Text),
Convert.ToInt32(txtReachable.Text));
}
private void oCounter_NumberReached(object sender, NumberReachedEventArgs e)
{
MessageBox.Show("Reached: " + e.ReachedNumber.ToString());
}
This is the syntax for initiating an event handler for some event: oCounter.NumberReached += new NumberReachedEventHandler(
oCounter_NumberReached);
Now, you understand what you are doing here! You are just instantiating an object of type And notice that we used It's because delegates are special objects that can hold references to more than one object (here, reference to more than one function). For example, if you had another function named oCounter.NumberReached += new NumberReachedEventHandler(
oCounter_NumberReached);
oCounter.NumberReached += new NumberReachedEventHandler(
oCounter_NumberReached2);
Now, after raising an event, both functions will be invoked one after another. If somewhere in your code, based on conditions, you decided oCounter.NumberReached -= new NumberReachedEventHandler(
oCounter_NumberReached2);
event KeywordA lot of folks keep asking this question: "what happens if we don't use In essence, declaring the In conclusion: an FinallyDon't forget to define these lines in your application's main constructor, instead of public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
oCounter = new Counter();
oCounter.NumberReached += new NumberReachedEventHandler(
oCounter_NumberReached);
oCounter.NumberReached += new NumberReachedEventHandler(
oCounter_NumberReached2);
The source code provided by this article is as above. If your vote is less than 5, let me know the reason ;-). I hope you will be satisfied after reading this article! History
|
||||||||||||||||||||||