Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8170583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:10:19+00:00 2026-06-06T21:10:19+00:00

I’ve been looking for several days for the solution to a trivial problem in

  • 0

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#?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:10:21+00:00Added an answer on June 6, 2026 at 9:10 pm

    Here is the solution I ended up with, based on the code in my initial post

    public class A: ZZZ { // this class is NOT under my control
       private b = new B(this);
       b.UserCallback = myCallback;
    
       static 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) {
          b.handle_event(e);
       }
    }
    
    public delegate void usercallback(Z z); // declare OUTSIDE both classes
    
    public class B {    // this class IS under my control
       public usercallback UserCallback = defaultCallback;
       static void defaultCallback(Z z) { /* do nothing */ }
    
       public handle_event(event e) {
           // do stuff...
           // then do the callback
           UserCallback(z);
       }    
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.