Is there a way to know in VB.NET if a handler has been registered for an event?

前端 未结 6 1391
难免孤独
难免孤独 2021-01-17 14:07

In C# I can test for this...

public event EventHandler Trigger;
protected void OnTrigger(EventArgs e)
{
    if (Trigger != null)
        Trigger(this, e);
}
         


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-17 14:28

    I believe the syntax I use for this in VB.Net is:

    Public Event Trigger As EventHandler
    
    
    Friend Sub OnTrigger(ByVal e As EventArgs)
        If TriggerEvent IsNot Nothing Then
            RaiseEvent Trigger(Me, e)
        End If
    End Sub
    

    Even though TriggerEvent does not appear to be declared the compiler will understand it.It closer emulates the C# syntax. Also I have read somehere that the code with that handler asssigned check runs fater, but I cannot point to that at the moment, so youcan take or leave that.

    I think that is the sytax, anyway. Please dont shoot me if it is not quite correct!

提交回复
热议问题