Vivek Thakur

Chaotically Complex

  Home  |   Contact  |   Syndication    |   Login
  102 Posts | 1 Stories | 316 Comments | 66 Trackbacks

News



Archives

ASP.NET Ventures

Many people initially get confused with the real need for delegates and events. I was one of them and it took me some time to figure it out :-). Recently answered a similar query in ASP.NET forums and thought it would be good if I create a blog post on this topic! Here was the query:

"I was reading an example somewhere of a Bank Class that if the minimum balance is reached you need to inform the rest of the app that the min has reached, but can't we do that by just calling a normal method.
for example: lets say when we deduct some amount from the balance and if minimum reached then call some method to take some action, I am totally missing why do we need delegates and custom events here?"

 Thing is in the Bank case, you can definitely call a method, but then it would be simple procedural programming, we need event based programming when we want our code to respond to some events generated by a system.

For eg.: think that windows OS is a system, and we are writing a code (in any language) where we want to capture an event like mouse_click(). Now how would our program know that a mouse click has occured? We can use low level code for it, but since OS is already handling low level code, its best to capture an event raised by the OS.

In other terms, the moment a mouse_click() happens the OS fires an event. The OS doesnt care who captures this event and uses it, it just sends out a notification. Then any code (like ours) can capture that event and use it accordingly. This saves us a lot of time to write code for the same ourselves. And other programs too can use the same event and handle it accordingly.

Similarly, the banking system can be huge, and many other outside applications might be accessing it. The banking system does not know how many such applications there are which need it, or are dependent on it, and how would they handle certain situations like when balance is low, so it simply fires an event whenever low balance occurs, and this event can be used by any other code, besides banking code itself.

Note that each susbcriber to that event can handle that event independently, for eg. the banking code might stop something from executing if balance is low, some other reporting app might send an email in such a case, or some ATM code can stop a particualr transaction and notify the user that balance is low.

Hope this clears things a bit!

posted on Tuesday, December 04, 2007 5:30 PM

Feedback

# re: Why do we need delegates and custom events? 1/6/2008 11:07 AM Ramesh. V
Hi,
I hope this blog on delegates and custom events would clear the mist that most of the .net beginners would have had.

# re: Why do we need delegates and custom events? 1/19/2008 12:22 PM manisha
ya thanks ur answer was too good. Put some more light on delegate also with some example. i am clear with events concept with ur explanation

# re: Why do we need delegates and custom events? 7/11/2008 1:36 PM Nikki
Nice and informatic Article.Keep it up :)

# re: Why do we need delegates and custom events? 9/8/2008 2:15 PM vipin
this is a good explanation. I also think, it will be great if you can explain more on the delegate part.

# re: Why do we need delegates and custom events? 9/30/2008 12:22 PM Kris
hi,
Ur article was excellent in terms of events-Delegates. I feel it would be gr8 if you can elaborate the delegate part alone. Even ur explanation can be like the following:

1. What is a delegate
2. Why do we go for a delegate
3. How to Use a delegate
4. When to go for a delegate

Expecting an update on ur article soon :)

Many Thanks
Kris

# re: Why do we need delegates and custom events? 11/30/2008 3:52 PM Hrishikesh
Hi,

This article is gr8. It answered most of the doubts in my mind.

It would be better if u could explain:
1. how to use delegates in custom control?
2. How they work internally?

Best Regards,
Hrishikesh



Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 1 and 7 and type the answer here: