You're likely trying to write this code in a class that is a descendant of the class that declares the ChatEvent
event. This can't be done,as events can only be treated as variables (including invoking them) in the class that declares them. This is because the event
keyword actually indicates to the compiler that it needs to perform some behind-the-scenes transformations.
What Happens
Consider this declaration:
Public Event MyEvent as EventHandler
Simple enough,right? What this actually does,however,is this (you just don't see it)
Private compilerGeneratedName as EventHandler
Public Event MyEvent as EventHandler
AddHandler(ByVal value as EventHandler)
compilerGeneratedName += value
End AddHandler
RemoveHandler(ByVal value as EventHandler)
compilerGeneratedName -= value
End RemoveHandler
RaiseEvent(ByVal sender as Object,ByVal e as EventArgs)
compilerGeneratedName.Invoke(sender,e)
End RaiseEvent
End Event
And when you invoke the event:
RaiseEvent MyEvent(Me,EventArgs.Emtpy)
It actually calls the code in the RaiseEvent
block.
Edit
If events in VB.NET can't be treated as variables anywhere (they can be treated as variables in the declaring class in C#,which is why your C# example compiles),then you'll have to explicitly implement the event yourself. See the MSDN page on the Event statement for more information on how to do that. To make a long story short,you're going to want some way to store multiple event handlers (or use a single event handler along with GetInvocationList
,as you're trying to do now). In your AddHandler
and RemoveHandler
code blocks you'll add to and remove from the list of event handlers (respectively).
You could use something like this:
Private myEventList as New List(Of EventHandler)
Public Custom Event MyEvent as EventHandler
AddHandler(ByVal value as EventHandler)
myEventList.Add(value)
End AddHandler
RemoveHandler(ByVal value as EventHandler)
myEventList.Remove(value)
End RemoveHandler
RaiseEvent(ByVal sender as Object,ByVal e as EventArgs)
For Each evt in myEventList
evt.BeginInvoke(sender,Nothing)
Next
EndRaiseEvent
End Event
So now,if you call
RaiseEvent MyEvent(Me,EventArgs.Emtpy)
It will raise the event in the fashion you're expecting.