I would like to do something like the following:
Public Class Form1
Public Event Evt1(ByVal c As c1)
Public Event Evt2(ByVal c As c2)
Private Sub OnEvt1OrEvt2(ByVal c As c1) Handles Me.Evt1, Me.Evt2
End Sub
End Class
Public Class c1
End Class
Public Class c2
Inherits c1
End Class
However, it seems to be invalid syntax, as the signature of OnEvt1OrEvt2 doesn’t match that of Evt2.
Is there any way to achieve this?
Edit: The above code seems to work fine for everyone but me, but it doesn’t compile for me in VS2005 SP1.
The error message is something like:
Error BC31029: The method ‘Private Sub OnEvt1OrEvt2(c As
WindowsApplication1.c1)’ can not handle
the event ‘Public Event Evt2(c As
WindowsApplication1.c2)’ because the
signatures do not match.
Edit 2:
I have found this in msdn,
vb-related:http://msdn.microsoft.com/en-us/library/ms973905.aspx
The code in the Form Load assigns
values to the Name property of the
three Bucket objects. It also calls
AddHandler to direct all of the
Overflowing events to the
HandleOverflow procedure. As you can
see, a single event handler can handle
multiple events, as long as all of the
events have compatible argument lists.(…)
Copyright © 2002 Informant
Communications Group and Microsoft
Corporation
(Emphasis mine)
No word about what two “compatible argument lists” exactly are, though.
Edit 3:
OK, I found it:
http://msdn.microsoft.com/en-us/library/ms364068%28v=vs.80%29.aspx#vb9overview_topic10
Relaxed Delegates
When creating a delegate using
AddressOf or Handles in Visual Basic
8.0, one of the methods targeted for binding to the delegate identifier
must exactly match the signature of
the delegate’s type.
It was added in VB9.
In order for this to work, you would need to define your event such that the parameter is passed as type Object, and then perform a casting operation within the event handler.
OR you could define an interface common to both C1 and C2, and use that as the parameter. You could then either use the reference “as-is” if the methods defined on the interface are suitable, or you would once again find yourself performing a casting operation.
Note that if you define an interface with the common methods, and if these common methods are all that is required for use by the event handler, then you do not need to perform any sort of “TypeOf” determination in order to handle the event.
All that said, I know that it is best, if possible, to follow the Event signature standard established within .NET wherever possible:
It looks to me like you might create a class (or classes) which inherits from System.EventArgs, and then define a custom property to access your custom parameter. This way, you can still pass a reference to the client object in the
Senderparameter, and then access your custom parameter thus: