I’ve been looking for several days for the solution to a trivial problem in C or C++ but seemingly impossible in C#.
Another programmer creates class A. An event handler method in class A needs to call a method in my class B to process the event. Depending on the type of event, that method in class B needs to callback a method in class A, and that method has an arbitrary name or may not even exist.
This is simple in C; you just have a pointer to the callback function and call it indirectly if it isn’t null. I don’t know how to do indirect method calls in C#. Here’s an example of some code illustrating the problem.
public class A: ZZZ { // this class is NOT under my control
private b = new B(this);
public void myCallback(C x) {
// do something
}
// Elsewhere in the application expects a protected
// override method to exist in *this* class A to handle
// an event. But we want a method in class B to handle
// it and then call myCallback depending on the type of event
protected override void handle_some_event(event e) {
// doesn't work -- how do I pass a "pointer" to the callback??
b.handle_event(e, myCallback);
}
}
public class B { // this class IS under my control
private A base;
public B(A a) {
base = a; // allows for calling methods in class A from class B
}
public handle_event(event e, ??? callback pointer ??? cback) {
// do stuff...
// then do the callback
// cback(); // this won't work
base.myCallback(); // this WILL work but only if I hard-code "myCallback"
}
}
The problem is that class B is the one I’m writing, and class A is authored by someone else who will be using my class B. That someone else has the option of not defining a callback at all, or creating one with any arbitrary name. Class B needs to know somehow what that is and how to access it. In C or C++ the other programmer could simply pass a pointer to his callback function. Is that possible in C#?
Here is the solution I ended up with, based on the code in my initial post
What got me stuck for a while is wrapping my head around the fact that a delegate declaration is analogous to a C function prototype, and that a delegate may be used as a declaration type in a class.