defining events in interfaces

  • Thread starter Thread starter Guest
  • Start date Start date
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?
 
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
 
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
 
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;
}
 
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
 
Back
Top