Understanding AddHandler and pass delegates and events.

Posted by Achilles on Stack Overflow See other posts from Stack Overflow or by Achilles
Published on 2010-03-17T21:29:55Z Indexed on 2010/03/17 21:31 UTC
Read the original article Hit count: 412

Filed under:
|
|
|
|

I am using AddHandler to wire a function to a control's event that I dynamically create:

Public Sub BuildControl(EventHandler as System.Delegate)

     dim objMyButton as new button

     AddHandler objMyButton.Click, EventHandler

end Sub

This code is generating a run-time exception stating:

Unable to cast object of type 'MyEventHandlerDelegate' to type 'System.EventHandler'

What am I not understanding about System.Delegate even though AddHandler takes as an argument of type "System.Delegate"? What Type does "EventHandler need to be to cast to a type that AddHandler can accept? Thanks for your help!

© Stack Overflow or respective owner

Related posts about .NET

Related posts about vb.net