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."
You need that kind of syntax in C# to ensure that you don't raise an event that has no subscribers. Necessary because that would crash your code with a NullReferenceException.
That's simply not necessary in VB.NET. And not supported, as you found out. Unlike C#, it supports the third accessor for events (beyond add and remove), it also supports the raise accessor. Which you invoke by, you guessed it, the RaiseEvent statement. It can deal with an event without subscribers just fine, no need to test.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With