Interfaces And Events

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Good Day All,

I have an application that processes several tasks. Depending upon the task
it instantiates the appropriate object to do the actual work. Since I wanted
the application to be extensible the processing objects all implement a given
interface. That way the process can load them dynamically at run time using
the Factory design pattern and adding new processing is as simple as
deploying the new worker assembly and updating the application's config file.

Here is my issue. I want each of the objects that implements the interface
to also implement an EventHandler for an Event on the main application. The
only way that I can see this as being guaranteed is by delcaring the
EventHandler in the interface. However, I can't figure out how to do it or if
I can. I know I can declare an Event in the interface but I am not sure how I
declare and EventHandler. Does anyone know if/how this can be done?

Thanks for the help!
 
Dan said:
I have an application that processes several tasks. Depending upon the task
it instantiates the appropriate object to do the actual work. Since I wanted
the application to be extensible the processing objects all implement a given
interface. That way the process can load them dynamically at run time using
the Factory design pattern and adding new processing is as simple as
deploying the new worker assembly and updating the application's config file.

Here is my issue. I want each of the objects that implements the interface
to also implement an EventHandler for an Event on the main application. The
only way that I can see this as being guaranteed is by delcaring the
EventHandler in the interface. However, I can't figure out how to do it or if
I can. I know I can declare an Event in the interface but I am not sure how I
declare and EventHandler. Does anyone know if/how this can be done?

You just declare it as any other delegate:

public delegate void MyEventHandler ([insert parameters here]);
 
Hi Dan,

There is no way to force a class to hook an event. What is the event
handler? It is a simply a method. It becomes event handler when it is used
to hook an event. While you can put that method in interface and the classes
implementing that interface have to provide implementation, you cannot force
hooking the event. What you might wanna do is insted of having an interface
to have abstract base class. In this class you can declare the event handler
as an abstract method. All classes that directly inherit from this class
have to override that method. The base class will be responsible of hooking
the event.

Sorry if I didn't get your problem correctly. I believe one simple example
could make your probem clearer.
 
Jon Skeet said:
You just declare it as any other delegate:

public delegate void MyEventHandler ([insert parameters here]);

Sorry, having read Stoicho's reply, I see what you're after, and my
answer wasn't anything like you need :(
 
Back
Top