Event and raisevent in

Posted on 2006-05-17
Last Modified: 2008-02-26
I would like to know the purpose of using Even and Raisevent instead of just building a function or procedure then call it by name?

Question by:jskfan
    LVL 12

    Assisted Solution

    Think of a ordinary Button for example. If it wouldnt raise a Click event, you could not add functionality to that.
    LVL 19

    Assisted Solution

    You have to call function manually by yourself but event handlers are automatically called whenever that event is raised and the handler is attached.

    to read details about event handling in, see:

    Author Comment

    Can you give me a simple example and go through its code and explain to me how Event/Raiseevent/Withevents are all related.

    The score is upgraded to 500 pts.

    LVL 85

    Accepted Solution

    We use events because it "decouples" classes from each other.

    If you used functions and wanted to call them directly then you must know exactly what kind of class contains the function and the exact name of the function.  Then you must have an actual reference to the object containing the function.  This often involves passing references from one class into another.  This makes the class less resuseable becuase it can only "operate" on classes that it already knows about (since you had to pass a reference into it with a specific type).

    If you instead make the class raise an event, then it doesn't need to know (and doesn't care) who or what kind of class receives that event.  The class can be reused somewhere else without change since it simply broadcasts its event and anyone who has subscribed using WithEvents or AddHandler() will be notified.

    Consider these four small classes:

        Public Class ClassA
            Public Sub FooA()
                MsgBox("FooA in ClassA")
            End Sub
        End Class

        Public Class ClassB
            Private myA As ClassA
            Public Sub New(ByVal clsA As ClassA)
                myA = clsA
            End Sub

            Public Sub FooB()
            End Sub
        End Class

        Public Class ClassC
            Public Sub FooC()
                MsgBox("FooC in ClassC")
            End Sub
        End Class

        Public Class ClassD
            Public Event FooD()

            Public Sub RaiseFooD()
                RaiseEvent FooD()
            End Sub
        End Class

    And then the following code:

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim a As New ClassA
            Dim b As New ClassB(a)

            ' versus...

            Dim c As New ClassC
            Dim d As New ClassD
            AddHandler d.FooD, AddressOf c.FooC
        End Sub

    ClassB is making ClassA do something by calling the method directly.
    ClassD is making ClassC do something by raising an event.

    But what if we wanted to make more than one class do something?

    With ClassB, we would need to pass in and store more references to each object that must do something.  Then we would have to add code to make the call on each class.

    With ClassD though, no changes need be made.  We simply use AddHandler to "wire" up other classes to receive the event in question.  This happens externall to ClassD.  So when ClassD raises its event, they ALL will be notified...and we didn't need to change ClassD in any way.

    Make sense?

    Author Comment

    I will test this later and get back with you.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
    This is an explanation of a simple data model to help parse a JSON feed
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    759 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

    12 Experts available now in Live!

    Get 1:1 Help Now