ラムダ式はこのイベント ハンドラから削除されません
更新 : 2007 年 11 月
エラー メッセージ
ラムダ式はこのイベント ハンドラから削除されません。ラムダ式を変数に割り当て、その変数を使用してイベントを追加および削除してください。
ラムダ式をイベント ハンドラで使用した場合、意図した動作にならない場合があります。コンパイラは、各ラムダ式の定義に対して、たとえそれが同一のものでも新しいメソッドを生成します。したがって、次のコードでは False が表示されます。
Module Module1
Sub Main()
Dim fun1 As ChangeInteger = Function(p As Integer) p + 1
Dim fun2 As ChangeInteger = Function(p As Integer) p + 1
Console.WriteLine(fun1 = fun2)
End Sub
Delegate Function ChangeInteger(ByVal x As Integer) As Integer
End Module
ラムダ式をイベント ハンドラで使用する場合、予期しない結果が発生する可能性があります。次の例では、AddHandler によって追加されたラムダ式が RemoveHandler ステートメントによって削除されません。
Module Module1
Event ProcessInteger(ByVal x As Integer)
Sub Main()
' The following line adds one listener to the event.
AddHandler ProcessInteger, Function(m As Integer) m
' The following statement searches the current listeners
' for a match to remove. However, this lambda is not the same
' as the previous one, so nothing is removed.
RemoveHandler ProcessInteger, Function(m As Integer) m
End Sub
End Module
既定では、このメッセージは警告です。警告を表示しない方法や、警告をエラーとして扱う方法の詳細については、「Visual Basic での警告の構成」を参照してください。
Error ID: BC42326
このエラーを解決するには
警告を回避し、ラムダ式を削除するためには、次の式に示されているように、ラムダ式を変数に割り当て、その変数を AddHandler ステートメントと RemoveHandler ステートメントの両方で使用するようにします。
Module Module1 Event ProcessInteger(ByVal x As Integer) Dim PrintHandler As ProcessIntegerEventHandler Sub Main() ' Assign the lambda expression to a variable. PrintHandler = Function(m As Integer) m ' Use the variable to add the listener. AddHandler ProcessInteger, PrintHandler ' Use the variable again when you want to remove the listener. RemoveHandler ProcessInteger, PrintHandler End Sub End Module