Introduction
As the programming languages evolved the reusability needs of the application also grew which leads to the evolution of a new programming paradigm. The paradigm shifted the whole world towards a new approach, The approach that gave us RAD ( Rapid application development) model and programming languages start jumping into the sea that followed. With Visual Basic Microsoft jumped into something that will in future take the developer friendliness to new highs. Visual basic to start with is an Object based, Event driven programming language. Object based means there are predefined objects that can be reused by the applications moreover this model gave developers ease to create and reuse the applications they use. These applications are event driven, which means the code, gets executed when some particular event with which the code is associated gets fired. Event driven programming follows Publisher-Subscriber model. This is same as you get subscription for a magazine. Every month magazine gets printed and the printing press has no idea whatsoever where the magazine is going. This magazine is the picked up by the vendor who has address of all the people who have taken subscription for it and it then delivers it to their doorsteps. In terms of programming this is what the picture looks like: -
Event à Delegate à Code
As you all know now that code gets executed when an event fires. Looks simple but while designing a TextBox ot lets say a Button controls, Do you have even a vague idea where will your control be placed. The answer is no. Object is a runtime entity and the method calls thus should be bind at rutime. Delegate contains the address of method to be bind to event.
Example
Button1.Click+=new EventHandler(Button1_Click);
Here we are attaching the click event of button to the Button1_Click method, which is done via delegate class, which in this particular case is System.EventHandler.
We can associate multiple functions also to a single event e.g
Button1.Click+=new EventHandler(Button1_Click); Button1.Click+=new EventHandler(Button2_Click); Button1.Click+=new EventHandler(Button3_Click);
This way all the three functions will execute when event fires and in this particular order only. We can also detach events using Button1.Click-=new EventHandler(Button3_Click);
Now some question arises, 1.) Can we attach any method to any event? Answer. NO. The signature of the function must match the delegate. 2.) what is the type of Click event here? Answer. Click event is of delegate type i.e. System.EventHandler here.
Lets have a look at the sinnature of Button1_Click function
protected void Button1_Click(object sender, EventArgs e) This also gives information about the delegate signature.The System.EventHandler delegate can only work with functions having return type void and accepting two parameters of type object and System.EventArgs respectively.
|
| Author: Mahesh Raj 07 Jun 2008 | Member Level: Gold Points : 1 |
This is very good information,Continue posting such useful articles.
|
| Author: Nanak Deep 18 Jun 2008 | Member Level: Bronze Points : 0 |
thank u sir
|
| Author: Ramkumar 20 Jun 2008 | Member Level: Bronze Points : 0 |
nice post
|
| Author: Bunty 21 Jun 2008 | Member Level: Diamond Points : 2 |
Hi, Very good aricle. It clearly explain what is event and delegate. You also explain with example that really nice. Keep posting such useful article.
|