VB MVP KenLin @HKSAR

Loving Microsoft Technologies.

Recent Posts

Tags

Community

Email Notifications

.NET Related

Other MVP

My Blogs in HK

.NET Insider

Archives

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)
         {
            SomeEvent(this, args);
         }

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
            SomeEventEvent(Me, args)
         End If

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.

==========================================================================================

Posted: Wed, Jan 31 2007 10:40 by kenlin | with 4 comment(s)
Filed under: ,

Comments

.NET MVP KenLin's Blog said:

Well, I start doing the translation again. I spent 2 days on this propose in the end of Jan. And I start

# March 20, 2007 3:51 PM

JP said:

Why does this not work? The AnyEventEvent is nothing, but why?

Public Class Form1

   Public Event AnyEvent As AnyEventHandler

   Public Delegate Sub AnyEventHandler(ByVal sender As Object, ByVal e As AnyEventArgs)

   Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click

       If Not AnyEventEvent Is Nothing Then

           AnyEventEvent(Me, New AnyEventArgs(0, 0, 0))

       End If

   End Sub

   Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

   End Sub

End Class

Public Class AnyEventArgs

   Private m_One As Integer

   Private m_Two As Integer

   Private m_Three As Integer

   Public ReadOnly Property One() As Integer

       Get

           Return m_One

       End Get

   End Property

   Public ReadOnly Property Two() As Integer

       Get

           Return m_Two

       End Get

   End Property

   Public ReadOnly Property Three() As Integer

       Get

           Return m_Three

       End Get

   End Property

   Public Sub New(ByVal _One As Integer, ByVal _Two As Integer, ByVal _Three As Integer)

       m_One = _One

       m_Two = _Two

       m_Three = _Three

   End Sub

End Class

# April 14, 2007 11:16 AM

Kirann Wanare said:

This document help me really in my aaplication

# June 22, 2007 12:24 AM

Thang said:

Mate! Thanks for the help. I was breaking my head to get this event corrected. Additing the "Event" to the end of the event's name worked like magic.

Cheers!!

# October 21, 2008 11:05 PM