By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,150 Members | 1,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,150 IT Pros & Developers. It's quick & easy.

Interfaces And Events

P: n/a
Dan
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!
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Dan <Da*@discussions.microsoft.com> wrote:
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]);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

P: n/a
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.

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"Dan" <Da*@discussions.microsoft.com> wrote in message
news:DB**********************************@microsof t.com...
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!

Nov 16 '05 #3

P: n/a
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
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 :(

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.