Assumed knowledge:
- Assume you know what a method is.
What is an event?
Let’s keep it simple. An event is anything which “happens”. “Oh great! That helps a lot :’( ,“ I hear you say. Examples are in order:
- The President comes to town. He could come at anytime. You don’t know when he could come. So when he comes, it’s an event.
- Somebody wins the lottery.
- A house catches fire.
- Somebody clicks a button.
All of these are events.
What happens next?
- Subscribers explained
What happens when an event happens? Anything you want!
Just say a house catches on fire. We will call this event “HouseFireEvent”. When this event is raised (i.e. when it happens), the following occur:
- People start screaming “OMMAGOD OMMAGAD!” (let’s call this the scream method)
- They call the fire department. (let’s call this the call-fire-department method)
- A fireman puts the fire out. (let’s call this the extinguish method).
In other words, the above methods are “called” when the house-fire event is raised. The house-fire event in fact maintains a list of methods which occur or are run when an event is raised. These methods are called subscribers. How can an event maintain a list of methods you ask? Good question. Usually there is a big book the fire warden in the floor of your department has tucked away in the back drawer of his desk. This book details all the protocols which must be followed when there is a fire. i.e. examples include: (i) staying calm, (ii) taking any valuables you have with you but no more, (iii) evacuating the building in an orderly and calm manner. These instructions, or “methods” are also known as “event handlers” because they are procedures which are called when a particular event occurs.
So in the above example, the event handlers would be: (i) Scream method, (ii) call-fire-department method and lastly (iii) the extinguish method.
………that’s it – it’s as simple as that!
Leave a Reply