The "RaiseEvent" in a Class

Posted on 2005-05-10
Last Modified: 2008-03-17
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?  

Question by:born4code
    LVL 62

    Expert Comment

    by:Fernando Soto
    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.
    LVL 85

    Accepted Solution

    >> What specific advantage does the "RaiseEvent" have over a simple function or procedure within a class?

    We're talking apples and oranges here...

    The purpose of RaiseEvent is to trigger an event that a class exposes to EXTERNAL subscribers...NOT internal subs/functions.  The direction is from within the class to outside the class.  It is in response to some kind of condition that the class is monitoring.  External subscribers to the classes events do NOT know when they will fire.

    On the other hand, if you call a sub/function that resides in a class then you have directed the class to do something.  The direction in this case is from outside the class to within the class.  The person making the call knows when the call was made because we explicitly called the sub/function ourself.

    Here is a simple example of a class that exposes two events and a form that has subscribed to them.  When Foo() in Class1 is called, the two events Even() and Odd() will be triggered in succession.  The external form does not know when these events will fire and is simply reacting to the fired events.  We trigger these events from within Foo() using the RaiseEvent keyword:

    ' -------------------------------------------------------------------------------------------
    ' Class1
    ' -------------------------------------------------------------------------------------------
    Public Class Class1

        Public Event Even(ByVal n As Integer)
        Public Event Odd(ByVal n As Integer)

        Public Sub Foo()
            Dim i As Integer

            For i = 1 To 6
                Select Case i Mod 2
                    Case 0
                        RaiseEvent Even(i)

                    Case 1
                        RaiseEvent Odd(i)

                End Select
        End Sub

    End Class

    ' -------------------------------------------------------------------------------------------
    ' Form1
    ' -------------------------------------------------------------------------------------------
    Public Class Form1
        Inherits System.Windows.Forms.Form

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim c As New Class1
            AddHandler c.Even, AddressOf Me.c_Even
            AddHandler c.Odd, AddressOf Me.c_Odd
        End Sub

        Private Sub c_Even(ByVal n As Integer)
            MsgBox(n, MsgBoxStyle.Information, "EVEN Number")
        End Sub

        Private Sub c_Odd(ByVal n As Integer)
            MsgBox(n, MsgBoxStyle.Information, "ODD Number")
        End Sub

    End Class

    LVL 19

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now