C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Events and Delegates


Posted Date: 16 May 2008    Resource Type: Articles    Category: .NET Framework
Author: shakti singh tanwarMember Level: Diamond    
Rating: Points: 10



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.




Responses

Author: Mahesh Raj    07 Jun 2008Member Level: Gold   Points : 1
This is very good information,Continue posting such useful articles.


Author: Nanak Deep    18 Jun 2008Member Level: Bronze   Points : 0
thank u sir


Author: Ramkumar    20 Jun 2008Member Level: Bronze   Points : 0
nice post


Author: Bunty    21 Jun 2008Member 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.



Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: A few C# .Net questions and answers!
Previous Resource: ASP.NET 2.0 Features
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use