• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 914
  • Last Modified:

RaiseEvent versus Delegate invoke method

Hi

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
andrea
0
andieje
Asked:
andieje
  • 2
1 Solution
 
parul11183Commented:
http://www.simple-talk.com/content/print.aspx?article=224

check out above site to get the concept clarity
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
0
 
andiejeAuthor Commented:
hi
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
Event-Handling-With-Delegates.doc
0
 
andiejeAuthor Commented:
thansk
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now