0

I keep getting the following message when I run code analysis.

CA1009 : Microsoft.Design : Declare the first parameter of 'xyz.EvaluateEventHandler' as an object named 'sender'

CA1009 : Microsoft.Design : Declare the second parameter of 'xyz.EvaluateEventHandler' as an EventArgs, or an instance of a type that extends EventArgs, named 'e'

How do I pass the variables, but also include sender and e to fix this violation?

'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
3
  • Event handler methods should not return a value. In the C# programming language, this is indicated by the return type void. To fix a violation of this rule, correct the signature, return type, or parameter names of the delegate. Commented Mar 8, 2015 at 0:47
  • possible duplicate of CA1009: Declare event handlers correctly? Commented Mar 8, 2015 at 2:02
  • 1
    This is not C# and it is not a duplicate of CA1009: Declare event handlers correctly. Either way, thank you for the down vote! Commented Mar 8, 2015 at 6:21

1 Answer 1

3

An event handler by convention accepts two parameters: a sender object and a parameter derived from EventArgs. That's what this code analysis rule is checking for. So your Evaluate event signature should look like this:

Public Event Evaluate(ByVal sender As Object, ByVal e As EvaluateEventArgs)

Your EvaluateEventArgs class could then contain the parameters you wish to pass to the listener.

Public Class EvaluateEventArgs
    Inherits EventArgs

    Public Property Np As String
    Public Property Test As Boolean
End Class

You would then raise it as:

Dim args As New EvaluateEventArgs()
RaiseEvent Evaluate(Me, args)
'...check args.Np, args.Test here if desired

And your event listener declaration would be:

Private Sub Evaluate(ByVal sender As Object, ByVal e As EvaluateEventArgs)
    Console.WriteLine(e.Np)
    Console.WriteLine(e.Test)
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.