Welcome to Pete Brown's 10rem.net

First time here? If you are a developer or are interested in Microsoft tools and technology, please consider subscribing to the latest posts.

You may also be interested in my blog archives, the articles section, or some of my lab projects such as the C64 emulator written in Silverlight.

(hide this)

Tip: Anonymous Event Handlers in VB.NET

Pete Brown - 16 April 2010

Back in 2009, I blogged about using Lambda expressions for async web service calls in C#, using an anonymous method. A comment on that blog tonight requested information on how to do that in VB.NET.

It has been about a million years since I wrote VB.NET code, so, I went and learned how to do it. Unfortunately, it appears that the lambda support in VB doesn't allow VB subs. I have it working with a delegate, but that's not quite the same thing.

Public Sub LoadPolicyDetail()
    Dim client As New Services.Service1Client()

    AddHandler client.GetPolicyDetailCompleted,
        Sub(s As Object, e As GetPolicyDetailCompletedEventArgs)
            If e.Result <> Nothing Then
                MessageBox.Show("Awesome!")
            End If
        End Sub

    client.GetPolicyDetailAsync()
End Sub

This version is not quite as trim as its C# lambda counterpart, as it's using a regular anonymous delegate rather than lambda, but it has the same result: avoids polluting your class with yet another method. In Visual Basic, Lambda expressions must be simple functions that return a value. That doesn't fly when creating an event handler.

     
posted by Pete Brown on Friday, April 16, 2010
filed under:      

10 comments for “Tip: Anonymous Event Handlers in VB.NET”

  1. William Wadesays:
    For VS 2010 this is no longer true. This months MSDN magazine ("What's New in Visual Basic 2010" pg 26) gives the example of

    AddHandler b.Click, Sub()
    MsgBox("Button Clicked!")
    End Sub

    Figured that might interest your readers.

    Wade
  2. Lloyd Sheensays:
    The code does not work in VS 2008. Without underscore chars it does not get too far but still is syntatically incorrect. If you are using VS 2010 it would be better to tell us that. I would really like it if what you posted would work
  3. Isaac Eckertsays:
    Silverlight's async web requests and binding is much easier to swallow with anonymous event handlers. The code looks less like spaghetti. If VB still has not caught up with C# 3.0 features by now then I think they will be in trouble when parallel processing really takes off. I have to program in VB.net every day wishing I had the features of C#. It seems that VB will continue to fall behind.

    If only it was up to me to decide whether to move to C# or not.
  4. William Wadesays:
    In the example they were showing that due to relaxed delegates you didn't need the parameters. Should have mentioned that, but the expanded example used:

    AddHandler b.Click, Sub(sender as object, e as EventArgs)
    MsgBox("Button Clicked!")
    End Sub

    All thanks to the new statement lambdas for VB 10.
  5. Petesays:
    @William

    Thanks. It's still different from the C# implementation which lets you omit the types but still have them resolved.

    I guess your point was that if you don't care about the parameters, you can omit them. However, on VB.NET, they really get omitted.

    Thanks

    Pete
  6. Ivan Ferrersays:
    I tried it this way in VS 2008 and it works:
    AddHandler Me.MouseClick, Function(o As Object, ee As MouseEventArgs) If(ee.Location.X > 600, MsgBox(ee.Location.ToString), Nothing)
    Where are going back the MsgBox result or the Nothing? I dont know.
  7. Ivan Ferrersays:
    I see my message posted and it looks like there are two lines of code, but there's only one.
    It doesn't matter if there are parenthesis or not enclosing the Function's code. It works OK in both ways.

Comment on this Post

Remember me