Visual Basic 2008 9.0 .NET Examples and Ebook

New in Visual Basic 2008 - 9.0

Introduction to Visual Basic

Volgend Onderwerp

Lambda Expressions

Vorig Onderwerp

Relaxed Delegates

|

Query Methods

Volgend Onderwerp
Delegates

Delegates

Lambda Expressions

Lambda Expressions

Definition and Implementation of a Lambda Function

Definition and Implementation of a Lambda Function

Nested Lambda Expressions

Nested Lambda Expressions

Relaxed Delegates and Lambda Expressions

Relaxed Delegates and Lambda Expressions



Delegates


Delegate-instances can point to a method.

(1) delegate1 of type SomeDelegate points to method SomeFunction.
(2) Identical to (1).
(3) delegate3 of type SomeDelegate ( local type inference ) points to method SomeFunction.
(4) delegate4 of type Func(Of Integer, Integer) point to method SomeFunction.
(5) Identical to (4à.
(6) 'delegate6' of type 'Func(Of Integer, Integer)' ( local type inference ) point s to SomeFunction'.

Local type inference makes sure variable delegate3 and delegate6 are strongly typed. Read the topic about local type inference for more details.


Option Infer On
Option Strict On
Public Class Example1
    Public Delegate Function SomeDelegate(ByVal value As Integer) As Integer
    Public Shared Sub Main()
        Dim value1 As Integer
        '
        Dim delegate1 As SomeDelegate = _
                                  New SomeDelegate(AddressOf SomeFunction) ' (1)
        value1 = delegate1.Invoke(5)
        Console.WriteLine(value1)
        '
        Dim delegate2 As SomeDelegate = AddressOf SomeFunction             ' (2)
        value1 = delegate2.Invoke(5)
        Console.WriteLine(value1)
        '
        Dim delegate3 = New SomeDelegate(AddressOf SomeFunction)           ' (3)
        value1 = delegate3.Invoke(5)
        Console.WriteLine(value1)
        '
        Dim delegate4 As Func(Of Integer, Integer) = _
                     New Func(Of Integer, Integer)(AddressOf SomeFunction) ' (4)
        value1 = delegate4.Invoke(5)
        Console.WriteLine(value1)
        '
        Dim delegate5 As Func(Of Integer, Integer) = _
                                                    AddressOf SomeFunction ' (5)
        value1 = delegate5.Invoke(5)
        Console.WriteLine(value1)
        '
        Dim delegate6 = _
                     New Func(Of Integer, Integer)(AddressOf SomeFunction) ' (6)
        value1 = delegate6.Invoke(5)
        Console.WriteLine(value1)
        '
        Console.ReadLine()
    End Sub
    Public Shared Function SomeFunction(ByVal value As Integer) As Integer
        SomeFunction = value * 2
    End Function
End Class
Download Broncode

Output :

 10
 10
 10
 10
 10
 10

The above example couldve left out SomeDelegate, after all the delegatevariables are objects of a generic instance of Func(Of T, TResult).

Read the topic about delegates for more information about typesafe methodpointers.


Klik hier om terug naar boven te gaan.  Up



Lambda Expressions


In Visual Basic 9.0 ( 2008 ) a "lambda expression" can be used where a delegate-instance is expected. The only exception is the RemoveHandler-statement.

A lambda expression defines the complete function. The definition is formed by keyword Function followed by the argumentslist and the expression that defines the returnvalue.


Public Class Example2
    Public Shared Sub Main()
        Dim value1 As Integer
        '
        Dim delegate1 As Example1.SomeDelegate = _
                                      Function(value As Integer) value * 2 ' (1)
        value1 = delegate1.Invoke(5)
        Console.WriteLine(value1)
        '
        Dim delegate2 As Func(Of Integer, Integer) = _
                                      Function(value As Integer) value * 2 ' (2)
        value1 = delegate2.Invoke(5)
        Console.WriteLine(value1)
        '
        Dim delegate3 = Function(value As Integer) value * 2               ' (3)
        value1 = delegate3.Invoke(5)
        Console.WriteLine(value1)
        '
        value1 = (Function(value As Integer) value * 2).Invoke(5)
        Console.WriteLine(value1)
        '
        Console.WriteLine((Function(value As Integer) value * 2).Invoke(5))
        '
        Console.ReadLine()
    End Sub
End Class
Download Broncode

Output :

 10
 10
 10
 10
 10
 10

(1) delegat1 of type SomeDelegate points to a lambda function, that expects an Integer argument and returns the doubled argumentvalue.
(2) delegate2 of type Func(Of Integer, Integer) points to an identical lambda function.
(3) delegate3 of an anonymous delegatetype, also points to an identical lambda function.


Klik hier om terug naar boven te gaan.  Up



Definition and Implementation of a Lambda Function


The body of a lambda function consists of only one expression, that expresses the returnvalue.
The returntype of a lambda function is derived from that expression using type inference.

The datatypes of the arguments are defined by the type specifiers of that arguments, or when omnited, are derived using type inference ( when possible ).
The lambda expression on lines (1) and (2) could leave out the type specifier for argument value. The type of value could been derived through type inference from the type of value of SomeDelegete (1) and from the type of T of the generic instance of Func(Of T, TResult) (2).
Option Strict On would not allow us to remove the type specifier of argument value on line (3). There is nothing to derived the type from.

Keywords ParamArray and Optional and generic arguments can not be used in the argumentslist of a lambda function.

Modifiers Overloads, Overridable, Overrides, Shadows, ... can not be used in the signature of lambda functions.


Klik hier om terug naar boven te gaan.  Up


Nested Lambda Expressions


Lambda expression can be nested.

In next example delegate1 points to a lambda function that returns the result of an other lambda function.

The expression delegate1.Invoke(2) points to the second lambda function, and also needs to be invoked.


Public Class Example4
    Public Shared Sub Main()
        Dim delegate1 = Function(arg1 As Integer) _
                            Function(arg2 As Integer) arg1 + arg2
        Dim value1 As Integer = delegate1.Invoke(2).Invoke(3)
        Console.WriteLine(value1)
        '
        Console.ReadLine()
    End Sub
End Class
Download Broncode

Output :

 5

Klik hier om terug naar boven te gaan.  Up


Relaxed Delegates and Lambda Expressions


Delegate-instances can point to methods with a slightly different signature than the signature defined by the delegatetype ( of that instance ).

In next example delegate1 of type SomeDelegate ( that would normally point to a function with an Integer argument ) can point to a function without arguments.

Read the topic about relaxed delegates for more details about these relaxations.

delegate2 of type SomeDelegate ( that normally points to a function that returns a Integer value ) can also point to a function that returns a value of a type that is convertable to Integer. The lambda function return a Short value. Short is convertable to Integer ( through widening conversion ), and therefore allowed under Option Strict On.
Conversion from Long to Integer is narrowing conversion, therefore line (1) is only allowed under Option Strict Off.

delegate4 of type SomeDelegate ( that normally points to a function with an Integer argument ) can also point to a function with an argument of a type from which Integer can convert to.
The lambda function works with an argument of type Long, Integer to Long is widening conversion, and therefore allowed under Option Strict On.
Conversion from Integer to Short is narrowing conversion, therefore line (2) is only allowed under Option Strict Off.


Public Class Example5
    Public Delegate Function SomeDelegate(ByVal value As Integer) As Integer
    Public Shared Sub Main()
        Dim delegate1 As SomeDelegate = Function() 1
        Console.WriteLine(delegate1.Invoke(123))
        '
        Dim delegate2 As SomeDelegate = Function() 2S
        Console.WriteLine(delegate2.Invoke(123))
        '
        'Dim delegate3 As SomeDelegate = Function() 3L                     ' (1)
        '
        Dim delegate4 As SomeDelegate = Function(value As Long) 4
        Console.WriteLine(delegate4.Invoke(123))
        '
        'Dim delegate5 As SomeDelegate = Function(value As Short) 5        ' (2)
        '
        Console.ReadLine()
    End Sub
End Class
Download Broncode

Output :

 1
 2
 4




This version ( published on 2008-06-24 ) is printed from http://www.studyvb.com, visit the website for more recent information.

Updated On : 2008-04-11

Download Broncode

Published On : 2008-06-24

Lambda Expressions

Vorig Onderwerp

Relaxed Delegates

|

Query Methods

Volgend Onderwerp

New in Visual Basic 2008 - 9.0

Introduction to Visual Basic

Volgend Onderwerp
Nederlands  Nederlands

Add to favorites (IE).