In C# I can test for this…
public event EventHandler Trigger;
protected void OnTrigger(EventArgs e)
{
if (Trigger != null)
Trigger(this, e);
}
Is there a way to do this in VB.NET? Test for null I mean?
MORE INFO
I forgot to mention. I have classes written in C# but I am writing my unit tests in VB.NET.
I am trying this in the unit test…
If myObject.Trigger IsNot Nothing Then
''#do something
End If
This is causing a compile time error which says … “Public Event Trigger is an Event and cannot be called directly. Use the RaiseEvent statement to raise an event.”
Seth
There is an interesting discussion in question 1129517 around how to do this very thing in C#.
Since the class that contains the Event was written in C#, the delegate semantics do apply, and those techniques should work for you. However, you’ll need to translate the source to VB.NET for your unit test.
Given the following class in a C# assembly:
Here is some VB.NET code which will correctly determine if a handler was registered for the Trigger event: