Lambda expression simplify code

One of the nice new features in Visual Basic 9 are lambda expressions. They where actually added as one of the underlying pieces for LINQ. But besides enabling LINQ you can use them in all sorts of places. One of the places they really help me is in places where I previously needed to declare a delegate and use that.

Take for example the following code where I use the ThreadPool to do some work:

Private Sub btnStart_Click() Handles btnStart.Click
    ThreadPool.QueueUserWorkItem(AddressOf DoWork)
End Sub

Private Sub DoWork(ByVal state As Object)

    For index As Integer = 1 To 100
End Sub

The SetProgressLabel function reports the progress to the user by setting the text on a label. As the work is done in a background thread I need to use Invoke to set the text property.

Before VB9 I would have to implement the SetProgressLabel like this:

Delegate Sub SetProgressLabelDelegate(ByVal percentage As Integer)
Private Sub SetProgressLabel(ByVal percentage As Integer)
    If lblStatus.InvokeRequired Then
        lblStatus.Invoke( _
            New SetProgressLabelDelegate( _
                AddressOf SetProgressLabel), _
        lblStatus.Text = percentage.ToString() + " %"
    End If
End Sub

First I would need to declare a delegate with the right signature, create an object passing in the address of the function to call and the parameters. Even though I declared the delegate with its parameters the Invoke is not aware of these and I can add make errors with the parameters which don’t show until runtime.

So here is a better way to do this in VB9:

Private Function SetProgressLabel(ByVal percentage As Integer) As Object
    If lblStatus.InvokeRequired Then
        lblStatus.Invoke(Function() SetProgressLabel(percentage))
        lblStatus.Text = percentage.ToString() + " %"
    End If
    Return Nothing
End Function

This time I use an lambda expression to call the SetProgressLabel function. No need for the extra delegate and I get full compile time checking [:)]. In fact the only drawback is that I need to declare SetProgressLabel as a function instead of a sub as VB forces a lambda expression to return a value. This makes working with lambda expressions somewhat less powerful than the C# implementation [:(] Lets just hope that problem is rectified soon.



One neat thing is that you don’t actually need to target the 3.5 framework to use lambda expressions. The previous sample works just fine targeting the .NET 2.0 runtime as long as VB9 is used. The reason for this is that the lambda expression syntax is actually just a compiler trick and it creates an delegate for us behind the scenes. Pretty cool!


6 thoughts on “Lambda expression simplify code

  1. Just let a thread that does not own the GUI controls/winForm set a property value.

    This has been needed from .NET 1.0 and is a much easier solution than forcing developers to a delegate or invoke() call.

    1. Provide an attribute for methods/properties on winform;s controls that lets them be set from a thread that does not own the control.

    2. When a thread that does not own the control attempts to set a property with this attribute, queue the set request and invoke automatically within the thread that owns the control.

    This will work almost every time and only requires special handling when the GUI thread is processing a message for a long long time.

    This will greatly simplify any multithreaded access to GUI controls.

  2. “One of the nice new features in Visual Basic 9 are lambda expressions. They where actually added as one of the underlying pieces for LINQ.”

    You wrote:
    “They where actually added…”


    “They were added…”

    Sorry to be so picky, but one thing I notice reading programmer blogs is the awful grammar. It detracts from the message. I do not mean to be so critical. It is embarrassing to be in this profession and see so examples of poor writing.

  3. Had to comment:

    ” It is embarrassing to be in this profession and see so examples of poor writing.”

    Dave probably should had heeded his own words and checked his own comment before posting.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>