Convert (If (SomeEvent != null)) into VB
As I said yesterday, I translated 3 labs on official WF HOLs from C# to VB2005. Today I start the lab04, I found out that there are some code that I cannot find a way to make it work in VB. So I start searching and found the following result in http://www.codeproject.com/vb/net/backgroundworker.asp . Following what he said and the code now is working.
This was not without its quirks.
The best practice for raising events tells us that we should always test that the event is not
null before raising the event. That is to say, the event must have handlers. All the articles then give a code sample something like this:
if(SomeEvent != null)
The C# implementations of
BackgroundWorker both used this standard. But how do I do that in VB.NET? If you try the statement
SomeEvent != Nothing, you get the error: 'Public Event SomeEvent(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event. Not very useful!
It turns out that in VB.NET, to get at the delegate derived object to test if it is
Nothing, you just add "Event" to the end of the event name. Don't go looking for it in intellisense though, because it won't be there! The resulting VB.NET code is:
if Not SomeEventEvent is Nothing Then
Why have I used
SomeEventEvent(Me, args) instead of
RaiseEvent SomeEvent(Me, args)?
The default method of a delegate is the
Invoke method, and
RaiseEvent in VB.NET just calls this method on the delegate, and it makes my code less language specific. Personal preference really.