I am trying to create a Sub that can take an object and add a load of event handlers for it, but the object can be any one of N types that all have these events. How can I do this? If I just have it as an Object as below AddHandler complains that the event does not exist for Object.
Can this be done?
Sub addHandlers(ByVal inputObject as object)
AddHandler inputObject.MouseDown, AddressOf HandleClick
AddHandler inputObject.MouseUp, AddressOf HandleMouseUp
End Sub
EDIT: Each of these objects is an instance of a class that inherits a different class (usually a control) that already has these events.
Thanks for any help,
Sam.
You probably just want to make
inputObjecta type that has all of these members. For example, if your using WinForms, it would be Control:Or if WPF, use
UIElement:Now any control that is of that type can be passed into
addHandlers.EDIT:
If there is no common type between all of them, you could use reflection:
The delegate type must still match though. This feels a bit clunky to me, though. If possible, try creating different overloads for
addHandlersas needed, and using base types to avoid creating many of them. If own the code of the types that can be passed in asinputOjbect, use an interface.