Jeff Reed Jeff Reed - 1 year ago 95 Question

How can I check if an event will be raised?

How can I check if an event will be raised in VB.NET? I seem to be able to do it in C#, but not in VB, even though I'm using the same syntax.

Here's my VB.NET syntax (which returns an error)

If [EVENT] IsNot Nothing Then
End If

And here's my C# syntax (works perfectly)

If([EVENT] != null) //If an event is not null(nothing)

Here's the error that the VB code returns

"[EVENT] is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event."

Answer Source

You have to use a strange and little known part of VB.Nets syntax which is that if you add Event to the end of an Events name it exposes the object in the same way as c# does so if you have an event Foo you can check if there are handlers with

If FooEvent IsNot Nothing Then
End If
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download