GAAnalytics4

2011-06-04

Lambda expressionda kendini(recursive) kullanım


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()

  1. Type of 'bol' cannot be inferred from an expression containing 'bol'

  2. 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:

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()
hata alınmaz.