defining events in interfaces

G

Guest

As far as I can see it is not possible to define an event in an interface.
(correct?)
This seems odd since the events that an object support are part of its
contract. It seem perfectly reasonable to define an interface that states
"this framework needs to have objects that support these methods and expose
these events".
I know I can do it via base class inheritance but I prefer not to do this
since it forces implementation on the implementers of the contract.
Did I miss something?
Any ideas on how to do it?
 
G

Guest

Who said it wasn't possible to define an event in an interface?
In VB.NET, I just use "Event EventName" inside the interface definition like
this:

Public Interface IBall
Event Bounce()
End Interface
 
R

Robert Jordan

Hi,
As far as I can see it is not possible to define an event in an interface.
(correct?)
This seems odd since the events that an object support are part of its
contract. It seem perfectly reasonable to define an interface that states
"this framework needs to have objects that support these methods and expose
these events".
I know I can do it via base class inheritance but I prefer not to do this
since it forces implementation on the implementers of the contract.
Did I miss something?
Any ideas on how to do it?

Of course you can:

interface IFoo {
event EventHandler Bar;
// or even
event EventHandler BarEnhanced { add; remove; };
}

bye
Rob
 
J

Jon Skeet [C# MVP]

pmoore said:
As far as I can see it is not possible to define an event in an interface.
(correct?)

Nope. For instance:

interface IFoo
{
event EventHandler X;
}
 
G

Guest

aha - brain freeze caused by try to compile
public interface foo
{
delegate FooEventHandler ...
event FooEventHandler FooEvent;
....
}

this is not allowed. Moved the delagte declaration elsewhere and all is fine
- thanks to all responders
 

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