What does RaiseEvent do

ryanlcs

Member
Joined
Apr 17, 2008
Messages
12
Programming Experience
Beginner
Hi

Newbie here, may I know what do RaiseEvent do?

It raise event. But what kind of event? And what does the event do?

Thanks.
 
It raises the event that you specify:
RaiseEvent MyEvent

The Event itself does nothing. It just is handled by any method that has the right signature and was set to handle it either via AddHandler or with the "handles" statement. Therefore that method is called an "Event handler".

An event is just a way to tell "somebody" (the handler(s) ) that "something" (whatever "meaning" the event might have) happened. It's a way to act in the moment when something happens without constantly checking IF it happened (that would be "polling" then)
 
Consider this. You are in your room writing some cool code on your computer while your mum is in the kitchen cooking dinner (sorry for the steroetype). What would you rather:

1. You leave your computer and go out to the kitchen every few minutes and ask your mum if dinner is ready.

2. Your mum calls you to let you know that dinner's ready.

It's the second one, right? Now, let's consider that there are several other people in the house waiting for dinner too. Should your mum have to tell each person individually that dinner's ready, or should she just yell out that it's ready and everyone who's listening will come for dinner? Again, it's the second one, right?

That's what events are like. Your mum just executed this code:
VB.NET:
RaiseEvent DinnerReady(Me, EventArgs.Empty)
Anyone who happens to be listening for that event now knows that dinner is ready and they can act accordingly, i.e. trundle out for dinner.
 
Back
Top