Passing EventHandler

C

Csharp Dummy

Hi,

I have a list of objects each with their own timer object and would
like them to use the same event handler function:
protected virtual void Generator_EventHandler(object Sender,
ElapsedEventArgs E)
{
// do something
}
For some reason I have problems passing the eventhandler to the
objects using the constructor.

Does anyone know how that works?
 
C

Csharp Dummy

Show the code.

An EventHandler is just a regular delegate type, and you can create an
instance of it and pass the reference to that instance to whatever
method you want, including a constructor, as long as the method has a
parameter taking that type or a type compatible with it (base type or
convertible, for example).  It works like any other type.

It's as simple as that.

So, unless you post the code you tried, with an explanation of what you
expected it to do, and what it did instead, including the exact text of
any error messages you may have gotten, it's not possible to know what
you're asking.  And it's not worth the effort to make guesses about what
you might be asking.

Pete

Pete,

Thank you that was it - I didn't create an instance.
Works now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top