开发者

How to call an event directly in a sub in vb.net

I try to implement a C# progr开发者_开发百科am 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();
    }
}


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜