Loading ...

Events and Delegates in C#.NET

Who is online?  0 guests and 0 members
home  »  blogs  »  Hajan Selmani  »  Events and Delegates in C#.NET

Product Spotlight

ASP.NET Hosting Spotlight

 

Comments (18)

SumitArora
Sumit Arora said:

Hajan,

The way you have explained delegates and events is amazing!!

Earlier I tend to get confused about how to use it but now it is clear when and where to use it.

Keep writing interesting blogs like this.

Thanks.

8/25/2010
 · 
 
by
mohit
mohit kumar said:

Hajan still I have one question in my mind. Can you please tell me why you declared 'event = null'  in constructor?

7/15/2010
 · 
 
by
mohit
mohit kumar said:

Thank you again Hajan:)

Hats off to you.

7/15/2010
 · 
 
by
mohit
mohit kumar said:

Keep writing and helping :)

7/15/2010
 · 
 
by
Vinz

This is "indeed" a great post! well presented. keep it up! =}

10/13/2010
 · 
 
by
mohit
mohit kumar said:

Thank you so much Hajan. Really it is very helpful for me and for who want to learn Delegate and events also.

It's my pleasure that u have written a blog on my requestSmile

 

7/15/2010
 · 
 
by
sushant
sushant said:

Hey Hajan its a very nice and helpfull post,
Thank you verymuch...!!! :)

6/6/2011
 · 
 
by
hajan
Hajan Selmani said:

I have posted this blog by request to one of the fellow members in CodeASP.NET Community - mohit, and I hope this was clear enough to help you understand the main concepts of events and delegates.

Please let me know your feedbacks.

7/14/2010
 · 
 
by
vivek_iit
Vivek Thakur said:

this is a very good example..good work! Also, write some articles on  anonymous functions too..another interesting topic...good work Hajan!

7/15/2010
 · 
 
by
vivek_iit
Vivek Thakur said:

this is a very good example..good work! Also, write some articles on  anonymous functions too..another interesting topic...good work Hajan!

7/15/2010
 · 
 
by
shaitender

nice post Cool

7/15/2010
 · 
 
by
hajan
Hajan Selmani said:

Guys, thank you for your comments! I really appreciate your thoughts!

Mohit, for your last comment/question:

The code inside constructor will run everytime the new object instance is created of the Numbers class. After that, as I have explained in the blog post:

Numbers MyNumb = new Numbers(); //1
EventHandlerClass myEvtCls = new EventHandlerClass(); //2
MyNumb.NumberFound += new DelegateFoundNumber(myEvtCls.NeededNumberFound); //3

when #1 runs, the constructor is called. It makes the NumberFound event null - clears all the references it contains to the delegates (there might be more than one delegates)

#2 we create instance myEvtCls (I've changed the name to shorer one)

#3 we connect the event with the method via delegate

in this case, if you delete the constructor, the code will run correctly. I have added this more as a concept I use :) - and also, firstly have been testing more complex solution and made it simpler in which we don't need the construcor, indeed. So, don't be confused about that ;).

I'm glad this was helpful.

7/15/2010
 · 
 
by
hajan
Hajan Selmani said:

@Sumit, your valuable comments give me stimulation to go further with contributing and writing blog posts like this one. Please, if you have any idea about any concept you would like to write about, post your thoughts. I have lots of things to write about, but I tend to go more with articles/blogs that are useful to all others in the community and the community guests. Thank you once again!

8/25/2010
 · 
 
by
zain ali
zain ali said:

very nice.

but i am still confused why you define delegate  DelegateFoundNumber  outside the class.

also please will you explain that when we invoke the event  NumberFound(c); how it calls the delegate automatically.

is this line  MyNumb.NumberFound += new DelegateFoundNumber(myEvtCls.NeededNumberFound);

is reponsible for this behaviour.

ASK LIKE AN EDIOT UNDERSTAND LIKE A GENIOUS.

10/13/2010
 · 
 
by
hajan
Hajan Selmani said:

Hey Vinzy...

Thanks for your comment :).

10/13/2010
 · 
 
by
raju
raju said:

this is the best article regarding delegates and events no doubt..after reading this i got the idea abt how delegate and events related to eacth other ..fantastic article..

10/25/2010
 · 
 
by
hajan
Hajan Selmani said:

Thank you for the comment and your questions @Zain Ali.

#1) The DelegateFoundNumber is defined outside the classes in order to be accessible from all other classes. Since I've defined the event (public event DelegateFoundNumber NumberFound;) inside the Numbers class and once this event happens, the delegate is invoked.

#2) You are partialy right since with MyNumb.NumberFound += new DelegateFoundNumber(myEvtCls.NeededNumberFound); we asign the method to be executed once the delegate is invoked and the delegate is invoked once the NumberFound event happens. So, NumberFound is an event that is fired once the condition inside the FindNumber() method from Numbers class are satisfied. The parameter that is passed to the NumberFound event is the parameter delegated by the delegate ;).

Hope this clears up your doubts.

If you have any more questions or doubts, please write back or you can open thread in the forum so that everyone can benefit from the chat :).

Thank you,
Hajan

10/13/2010
 · 
 
by
  • Name:*
  • Email:*
  • Website:
Type the characters you see in the image *

Confirm

Latest community blog posts

  • While working with one of the sample application I noticed that form authentication timeout in web.config was overriding the manual ticket's (which we created via code) timeout. Below is the code which was written in the application: var ticket = new FormsAuthenticationTicket(1, "UserName", DateTime...
  • In this blog I will show how to select a range of characters in textbox with jquery. In my demo I have used one textbox and one button. On click of button arange of characters in textbox will be selected. Below is the sample code: <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="ser...
  • Recently we moved from Newtonsoft.Json 3.5 to 4.0 version. We noticed that we were getting Could not load file or assembly 'Newtonsoft.Json, Version=3.5.0.0.. while working with Twitter authentication which we were using in our application. The third party dll which we were using for Twitter authent...
  • PRODUCT SPOTLIGHT This is an addition to my series of blog posts on jQuery Mobile: jQuery Mobile Introduction & Tips To Get Started jQuery Mobile Getting Started So, if you've been following my posts so far, you should have a good overview of jQuery Mobile, it's documentation, how to set up your...
  • add message icon with count displayed on it as new messages come into my gridview. i want to do this in asp.net for web application.

Hajan Selmani's latest blog posts

Product Spotlight

ASP.NET Hosting Spotlight

 

Quick Vote

What kind of email newsletter would you prefer to receive from CodeAsp.Net?