Link to home
Start Free TrialLog in
Avatar of born4code
born4code

asked on

The "RaiseEvent" in a Class

Does anyone know what this is good for?  I have never used this because I can easily just put a function or procedure in my class and call it directly.  What specific advantage does the "RaiseEvent" have over a simple function or procedure within a class?  

Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Within the same class not much. As you stated it is easier just to call a function. But lets say you create a control or extend one, and in this control ( button, list box, radio button, or something of your own creation) and you want to inform the user of your control that something happened that needs there attention or action. The control will raise an event and the user of the control can listen for its event. So lets take an example. A user clicks a button the button code raises a button click event and your code responds to that event.

I hope this was of some help.
 
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of arif_eqbal
arif_eqbal

Well "RaiseEvent" is a very special thing even in a class.
Actually it is meant to raise your custome events. As FernandoSoto said it is particularly useful in a Control, But elsewhere even in a normal class it is sometimes too good.

As I said it is a way to raise your custom events,So its primary use is to raise your user defined events. Now often we don't really understand the power of Events and we tend to under-utilize the Events.

As you said >>> "I have never used this because I can easily just put a function or procedure in my class and call it directly.  What specific advantage does the "RaiseEvent" have over a simple function or procedure within a class"

Yes you can easily put a Function/Procedure but then this Function/Procedure needs to be called by the other class using this class. But in case of events its (well almost) the other way round, now instead of they calling your Function/Procedure you are calling their Function/Procedure. That's the specific Advantage of RaiseEvent.

You understand ??? Say you have a Class MyClass it has some functions and one event.
And say I am using your class, Now it's in my hands when I call your method, I mean when the flow goes to that particular function call the Function in your class will be called, you have no control when. Now suppose you want that at a particular time (event) some code should run in the client calling your class, How'd you do it?? You'll provide with an event and call RaiseEvent so that some code in the client program can be run.

A judicious use of Events/RaiseEvents gives a good class design, A lot of people now a days stress on "Loosely Coupled Architecture". Events/Delagates are the key to such a Design.