I am trying to learn some WCF principles by following an example of a WCF application (from Sacha Barber).
Now I would like to convert the following function into VB.NET
private void BroadcastMessage(ChatEventArgs e)
{
ChatEventHandler temp = ChatEvent;
if (temp != null)
{
foreach (ChatEventHandler handler in temp.GetInvocationList())
{
handler.BeginInvoke(this, e, new AsyncCallback(EndAsync), null);
}
}
}
but I have some problems, because the following code is not accepted by the compiler
Private Sub BroadcastMessage(ByVal e As ChatEventArgs)
Dim handlers As EventHandler(Of ChatEventArgs) = ChatEvent
If handlers IsNot Nothing Then
For Each handler As EventHandler(Of ChatEventArgs) In handlers.GetInvocationList()
handler.BeginInvoke(Me, e, New AsyncCallback(AddressOf EndAsync), Nothing)
Next
End If
End Sub
it says
Public Shared Event ChatEvent(sender As Object, e As ChatEventArgs)' is an event, and cannot be called directly
Coming to the point, is it then possible in VB.NET get the handlers linked to a certain event in some other way?
use ChatEventEvent (or EventNameEvent)
It won't show up in intellisense, but the members of it will.
VB.NET creates a variable behind the scenes, to hide the complexity from the coder...
This is only available in the class that declares the event (or perhaps its descendants)
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 theevent
keyword actually indicates to the compiler that it needs to perform some behind-the-scenes transformations.What Happens
Consider this declaration:
Simple enough, right? What this actually does, however, is this (you just don't see it)
And when you invoke the event:
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 yourAddHandler
andRemoveHandler
code blocks you'll add to and remove from the list of event handlers (respectively).You could use something like this:
So now, if you call
It will raise the event in the fashion you're expecting.