custom Event question

ThomasT

Member
Joined
Aug 7, 2010
Messages
11
Programming Experience
1-3
i have Programed a dll that provides IRC functionality in vb.net and it works fine but when i run code analyses on my code it doesn't like my custom events even though they work

VB.NET:
Public Event TextRecvevied(ByVal sender As Object, ByVal e As EventArgs, ByVal Str As String)

and the warning is

Warning 3 CA1009 : Microsoft.Design : Remove all but the first two parameters of 'Irc.TextRecveviedEventHandler'.
i can't see how i can make my program work if i take away my third parameter
the event is raised from a bacgroundworker progress changed
should i listen to the warnings and find another way or is my way valid ?
 
Design guidelines recommend all events have signature sender As Object, ByVal e As EventArgs (or derived) - where sender identifies the class instance that raised the event and e provides any additional event info. You can inherit EventArgs class and add properties to hold your extra information, for example:
VB.NET:
Public Class InfoEventArgs
    Inherits EventArgs

    Public Property Info As String
End Class
Now you can define the event like this using the generic EventHandler delegate:
VB.NET:
Public Event TextReceived As EventHandler(Of InfoEventArgs)
The old EventHandler delegate has the signature mentioned first, and this generic delegate that was added in .Net 2.0 makes the EventArgs type dynamic. This saves you typing out the full event signature, or even defining the event delegate if this is needed. In case you wondered, the above signature is equivalent of this (where IDE generates the delegate for you):
VB.NET:
Public Event TextReceived(sender As Object, e As InfoEventArgs)
When you add an event handler you'll see they both give the same result.

Here is the event design guidelines help page: Event Design
 
If the information provided by JohnH is not enough, you might like to follow the Blog link in my signature and check out my post on Custom Events. It goes over various aspects, including passing data to the event handler via a custom EventArgs class.
 
Back
Top