Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

The "RaiseEvent" in a Class

Posted on 2005-05-10
3
Medium Priority
?
576 Views
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?  

0
Comment
Question by:born4code
3 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 13974759
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.
 
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 13974970
>> 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
        Next
    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
        c.Foo()
    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

0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 13975021
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

578 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