RemoveHandler and lambda expressions

msch-prv

New member
Joined
Jul 2, 2011
Messages
2
Programming Experience
1-3
Hi, Does someone know how to remove a handler set up by a lambda expression? According to this MSDN article, lambda expression should be assigned to variables which should be used to define handler operators. So far, I have used lambda expessions w/o variables. For instance, the following works:

Private Sub ScaleObjectInit1(ByVal objSrc As NumericUpDown, ByVal objTgt As FrameworkElement)
...
AddHandler Me.upDnScale.ValueChanged, Sub(sender As Object, e As Object) ScaleObject1(sender, e, objTgt)
End Sub

Private Sub ScaleObject1(ByVal sender As Object, ByVal e As Object, ByVal objTgt As FrameworkElement)
....
End Sub



Removing this handler via the following raises a compiler error as explained in the MSDN ref:

RemoveHandler Me.upDnScale.ValueChanged, Sub(sender As Object, e As Object) ScaleObject1(sender, e, objTgt) ' Does not work



I tried the following to no avail:

Event ProcessScale(ByVal sender As Object, ByVal e As Object, ByVal el As FrameworkElement)
Dim ScaleHandler As ProcessScaleEventHandler
..
AddHandler ProcessScale, ScaleHandler ' Does not work
AddHandler Me.upDnScale.ValueChanged, ScaleHandler ' Raises signature conflict



Any ideas would be greatly appreciated. TIA.
 
As article explains keep a reference to the delegate instance that was assigned AddHandler and RemoveHandler same instance later.
Raises signature conflict
Signature for event, delegate and handler must match.
Your event signature is non-standard and should be changed, but using it as it is doesn't prevent it from work correctly. Standard signature for events is (sender As Object, e As EventArgs_Derived)
 
I found a solution: the work-around is to declare the handler as the same type as the control event delegate. In my app (SL4), the upDnScale control is of type NumericControl, so its delegate type RoutedPropertyChangedEventHandler is already known to the compiler. I somewhat overlooked this fact by studying the MSDN example. The code to add/remove a lambda event listener is shown below.

' Start/End scaling process
Me.ScaleObjectBeg(Me.upDnScale, _selElem)
...
RemoveHandler Me.upDnScale.ValueChanged, ScaleObject2Handler

' Handler declaration and processing subs
Dim ScaleObject2Handler As System.Windows.RoutedPropertyChangedEventHandler(Of Double) ' Event handler variable. (Delegate type already exists,)

Private Sub ScaleObjectBeg(ByVal objSrc As NumericUpDown, ByVal objTgt As FrameworkElement)
...
ScaleObject2Handler = Sub(sender As Object, e As Object) ScaleObject2(e, objTgt) ' Assign lambda expr. to event hdlr. var.
AddHandler Me.upDnScale.ValueChanged, ScaleObject2Handler
End Sub

Private Sub ScaleObject2(ByVal e As Object, ByVal objTgt As FrameworkElement)
...
End Sub
 
That is also true, you use the delegate the event defines.
e parameter of RoutedPropertyChangedEventHandler(Of Double) is strictly RoutedPropertyChangedArgs(Of Double) rather than Object, but it will pass due to relaxed delegates that was introduced previous VB version. If you don't need the event parameters they can also be omitted. Parameter types can also be omitted, since they can be inferred.
 
Back
Top