Developers conversant in the event-driven programming model of MS-Windows and the X Window System are accustomed to passing function pointers that are invoked (that is, "called back") when something happens. object-oriented model does not currently support method pointers, and thus seems to preclude using this comfortable mechanism
Interfaces provides a mechanism by which we can get the equivalent of callbacks. The trick is to define a simple interface that declares the method we wish to be invoked.
Interfaces provides a mechanism by which we can get the equivalent of callbacks. The trick is to define a simple interface that declares the method we wish to be invoked.
public interface InterestingEvent
{
// This is just a regular method so it can return something or
// take arguments if you like.
void interestingEvent();
}
public class EventNotifier
{
private InterestingEvent ie;
//private bool somethingHappened;
public EventNotifier(InterestingEvent e)
{
Console.Write("EventNotifier called\n");
// Save the event object for later use.
ie = e;
// Nothing to report yet.
e.interestingEvent();
}
}
public class CallMe : InterestingEvent
{
private EventNotifier en;
public CallMe()
{
// Create the event notifier and pass ourself to it.
en = new EventNotifier(this);
}
// Define the actual handler for the event.
public void interestingEvent()
{
Console.Write("Call Me function called");
}
//...
}
//*****Client Side**************
CallMe obj = new CallMe();
Console.ReadKey();
//******End***************
No comments:
Post a Comment