0

I try to implement a C# program in VB.NET and I've stuck on a point. I would like to get the exact equivalent. I could not find out how to pass an event to a sub or function in the following way. So the part which I cannot do in vb.net is var mediaOpenedHandler = MediaOpened;. In VB you cannot pass an event like this. If I write in VB: Dim mediaOpenedHandler = MediaOpened then it says "'Public Event MediaOpened()' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event." So how can I get exactly the same result. Thanks in advance.

public abstract class MediaPlayerBase : WorkDispatcherObject
{
…
public event Action MediaOpened;
…
protected void InvokeMediaOpened()
    {
        var mediaOpenedHandler = MediaOpened;
        if (mediaOpenedHandler != null)
            mediaOpenedHandler();
    }
}
Gabe Miller
  • 132
  • 2
  • 10

2 Answers2

0

of the top of my head, it is literally just RaiseEvent mediaOpenedHandler()

hermiod
  • 1,158
  • 4
  • 16
  • 27
  • Thanks hermiod for your answer. But then where is the connection between the public MediaOpened actiona and the raised mediaOpenhandler? In C# it says mediaOpenedHandler = MediaOpened. I admit, I got confused. I found this code pattern many places in the original code and it seems as if the programmer just would like to somehow "rename" the event. – Gabe Miller May 28 '11 at 16:23
  • So anybody has any idea how to pass an event to a local variable in VB? Or what is my major misunderstanding here? – Gabe Miller May 28 '11 at 23:44
0

I found the answer myself here Event handler raising method convention. So basically as hermiod answered you just use RaiseEvent in VB. In C# they have to use this local assignment for this purpose: "In a multi-threaded application, you could get a null reference exception if the caller unregisters from the event. The assignment to a local variable protects against this." In VB the RaiseEvent handles this internally.

Community
  • 1
  • 1
Gabe Miller
  • 132
  • 2
  • 10