运行代码分析时,我不断收到以下消息。
CA1009:Microsoft.Design:将'xyz.EvaluateEventHandler'的第一个参数声明为名为'sender'的对象
CA1009:Microsoft.Design:将'xyz.EvaluateEventHandler'的第二个参数声明为EventArgs,或者扩展EventArgs的类型的实例,名为'e'
如何传递变量,还包括发件人和e来修复此违规行为?
'xyx class
Public Event Evaluate(ByVal np As String, ByRef test As Boolean)
RaiseEvent Evaluate(Np, test)
'NpHandler class
AddHandler _xyx.Evaluate, AddressOf Evaluate
Private Sub Evaluate(ByVal np As String, ByRef test As Boolean)
Console.WriteLine(np)
Console.WriteLine(test)
End Sub
答案 0 :(得分:2)
事件处理程序by convention接受两个参数:sender
对象和从EventArgs
派生的参数。这是代码分析规则要检查的内容。因此,您的Evaluate
事件签名应如下所示:
Public Event Evaluate(ByVal sender As Object, ByVal e As EvaluateEventArgs)
然后,您的EvaluateEventArgs
类可以包含您希望传递给侦听器的参数。
Public Class EvaluateEventArgs
Inherits EventArgs
Public Property Np As String
Public Property Test As Boolean
End Class
然后你会把它提升为:
Dim args As New EvaluateEventArgs()
RaiseEvent Evaluate(Me, args)
'...check args.Np, args.Test here if desired
您的事件监听器声明将是:
Private Sub Evaluate(ByVal sender As Object, ByVal e As EvaluateEventArgs)
Console.WriteLine(e.Np)
Console.WriteLine(e.Test)
End Sub