RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


What's New in Visual Basic 9.0? Part 1 : Page 5

The release of Visual Studio 2008 updates Visual Basic to version 9.0, with several key language enhancements and LINQ support. Part one of this series will walk you through using nullable types, type inference, the If operator, and other time-saving enhancements.

Lambda Expressions
In VB 9.0, a Lambda expression is a nameless function that evaluates a single expression and returns its value. Consider the following function:

    Private Function CubeRoot(ByVal num As Single) As Double
        Return Math.Pow(num, 1 / 3)
    End Function
Before, you'd call the CubeRoot() function using this argument:

        MsgBox(CubeRoot(8)) '---returns 2---
Using a Lambda expression, you can rewrite and call the CubeRoot() function as follows:

        Dim CubeRoot = Function(num As Single) Math.Pow(num, 1 / 3)
        MsgBox(CubeRoot(8)) '---returns 2---
A Lambda expression is defined using the Function keyword, followed by its parameter and then the single expression within the function. Note that it is optional to specify the parameter type and there is no need to specify the return type. The above Lambda expression can be rewritten as:

        Dim CubeRoot = Function(num) Math.Pow(num, 1 / 3)
The above Lambda expression uses type inferencing to determine the Lambda expression. You can also explicitly define a Lambda expression using the Func keyword, like this:

        Dim CubeRoot As Func(Of Single, Double) = _
           Function(num As Single) Math.Pow(num, 1 / 3)
Here, you use the Of keyword to specify the type of parameter(s) and their return type. The rightmost parameter always specifies the return type; in this case, the return type is Double. The parameters on the left then specify the input parameters. In this case, there is only one input parameter of type Single.

The following Lambda expression has no input parameters and returns a Boolean value:

Dim ReturnTrue As Func(Of Boolean) = Function() True
The following Lambda expression has two input parameters of type Integer and returns a Double value:

Dim Multiply As Func(Of Integer, Integer, Double) = Function(a, b) a * b
A Lambda expression can also return another Lambda expression. Consider the following expression:

        Dim AddConstant = Function(constant) Function(y) constant + y
To better understand the above Lambda expression, rewrite it as follows:

        Dim AddConstant = Function(constant) _
                             Function(y) constant + y
As you can see, the AddConstant expression takes in a single argument and returns another Lambda expression, which is:

                             Function(y) constant + y
To use the AddConstant expression, you can call it using the following:

        Dim z = AddConstant(4)
        MsgBox(z(7)) '---prints out 11---
Here, z is another function that you can call with a single argument. By calling the AddConstant function with the argument 4, z is essentially:

                             Function(y) 4 + y
Therefore, whatever value you pass into z will be added to the value 4, which in this case the return value is 11.

Lambda expressions are useful in cases where you need to pass in a function for a parameter whose type is delegate. For example, suppose you have an array of controls:

        Dim allControls As Control() = _
           {Button1, CheckBox1, Button2, CheckBox2}
To retrieve all the CheckBox controls within this array, you can use one of the many extension methods available for the array (see Figure 5).

Figure 5. Methods: The List of extension methods for an array.
Figure 6. View the Connection Details: After specifying the database properties, you should see the data connection details.

The Where() extension method takes in a Lambda expression that allows you to test each element of the array for a specific condition (see Figure 6).

You can use the following Lambda expression to retrieve all the CheckBox controls in the array:

    Dim checkBoxes = allControls.Where(Function(c) TypeOf c Is CheckBox)

More to Come!
This is only a partial list of the new features in VB 9.0. Be sure to download the trial edition of Visual Studio 2008 (or download the various free Expression editions) if you have yet to experience the power of all these language extensions. And stay tuned for Part 2 to delve into the opportunities provided by VB 9.0's LINQ support.

Wei-Meng Lee is a Microsoft MVP and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies. He is an established developer and trainer specializing in .NET and wireless technologies. Wei-Meng speaks regularly at international conferences and has authored and coauthored numerous books on .NET, XML, and wireless technologies. He writes extensively on topics ranging from .NET to Mac OS X. He is also the author of the .NET Compact Framework Pocket Guide, ASP.NET 2.0: A Developer's Notebook (both from O'Reilly Media, Inc.), and Programming Sudoku (Apress). Here is Wei-Meng's blog.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date