RaiseEvent versus Delegate invoke method

Posted on 2009-04-16
Last Modified: 2012-05-06

I am trying to understand the delegate model of event handling. I was hoping someone would confirm or disconfirm what I believe to be the case.

step 1. I have a class that defines the event arguments if required
step 2. I have a class that declares the delegate. This in essence specifies the signature that methods that handle the event must have. The delegate method has 2 parameters: the event sender object and the event args
step 3. I have a class that defines the event. This class also have a method that raises the event. The method is normally called OnEvent

In step 3 i believe when i write something like

Public Event Authenticate As FormsAuthenticationEventHandler

The 'As FormsAuthenticationEventHandler' part of the statement is specifying what class acts as a delegate for this event. Is this correct?

Also in step 3, in the method called OnEvent I believe i can raise the event in 2 ways.
Firstly I could call the RaiseEvent statement. Secondly I believe that when you define an event member in your class using the event keyword the compiler a private member such as:

private EventNameHandler _eventHandler = null;

meaning i could raise the event by calling Me._eventHandler.Invoke(Me, e) where e is the event args that are a parameter to the method

An example of the second method is below

Private Sub OnAuthenticate(ByVal e As FormsAuthenticationEventArgs)
        If (Not Me._eventHandler Is Nothing) Then
                Me._eventHandler.Invoke(Me, e)
        End If

Is my understanding correct so far?

My last question is, what is the difference if any in raising the event by RaiseEvent of the Invoke method

many thanks for your help
Question by:andieje
    LVL 1

    Expert Comment


    check out above site to get the concept clarity
    LVL 85

    Accepted Solution

    Which LANGUAGE are you working in here?  You have mixed C# with VB.Net code...

    It is confusing because custom events look very different in the two languages.

    In VB.Net, you don't actually need any delegates.  (I personally like the VB.Net syntax.)

    Step #1: Define the Event and its Arguments at the same time.
    (You can specify whatever arguments you want!)

        Public Event NewMessage(ByVal msg As String, ByVal TimeStamp As DateTime)

    Step#2: Raise the Event:
    (Obviously you can replace the literals below with appropriate variables.)

        RaiseEvent NewMessage("Hello World!", DateTime.Now)

    That's it!  Any handler that has subscribed to NewMessage() will be called with the passed parameters.

    Author Comment

    sorry i mixed the language. the syntax of the language wasn't what was important to me. It was the general concept of the events and delegates. I wrote some notes which I think are correct. I've attached them

    Author Closing Comment


    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    731 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

    19 Experts available now in Live!

    Get 1:1 Help Now