Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if an event will be raised?

Tags:

vb.net

events

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."

like image 596
Jeff Reed Avatar asked Mar 22 '23 09:03

Jeff Reed


1 Answers

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.

like image 115
Hans Passant Avatar answered Mar 28 '23 14:03

Hans Passant