Visual 2008 VB ile gelen Lambda expression ile artık kod içinde istediğimiz yerde Function,Sub kullanabiliyorduk.Örneğin:
Private Sub UCAgac_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
Dim action = Sub()
MessageBox.Show("Deneme")
End Sub
Dim bol = Function(a As Double)
Return a / 4
End Function
End Sub
Eğer bu şekilde tanımlı bir lambda expression kendi içinde çağırmak istediğimizde
Dim bol = Function(a As Double)
If a > 0.0111 Then
Return bol(a / 4)
Else
Return a / 8
End If
End Function
Dim t As New System.Windows.Threading.DispatcherTimer()
t.Interval = TimeSpan.FromMilliseconds(100)
Dim handler = Sub()
If 1 > 0 Then
RemoveHandler t.Tick, handler
End If
End Sub
AddHandler t.Tick, handler
t.Start()
- Type of 'bol' cannot be inferred from an expression containing 'bol'
- Type of 'handler' cannot be inferred from an expression containing 'handler'
Visual Studio şeklinde bize bir hata verecektir.Sebebi ise lamda expressionlarının açık şekilde
tanımlanmamasıdır.Eğer açık şekilde tanımlarsak:
hata alınmaz.
Dim bol As System.Func(Of Double, Double) = Function(a As Double)
If a > 0.0111 Then
Return bol(a / 4)
Else
Return a / 8
End If
End Function
Dim t As New System.Windows.Threading.DispatcherTimer()
t.Interval = TimeSpan.FromMilliseconds(100)
Dim handler As System.EventHandler = Sub(sender As Object, e As System.EventArgs)
If 1 > 0 Then
RemoveHandler t.Tick, handler
End If
End Sub
AddHandler t.Tick, handler
t.Start()