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

  • Home
  • SEARCH
  • 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 4610798
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:10:55+00:00 2026-05-22T01:10:55+00:00

I have two very similar classes that do essentially the same thing. The only

  • 0

I have two very similar classes that do essentially the same thing. The only difference is in a callback handler provided to an instance of each class. The callback handlers are different and they are accepted with different parameters. I would like to generalize most of the code from these classes into a base class. Any ideas on how to generalize the delegate code intelligently? I’m on .NET 2.0

Note: I read this very useful blog on inheritance with delegates and articles on covariance and contravariance with delegates, but I still don’t see how that knowledge can be applied here.


public class A
{
    public delegate void AHandler(string param1, string param2);
    public void AcceptHandler(string param3, AHandler handler);
    public void InvokeHandler(string forParam1, string forParam2);

    // the rest is same
}

public class B
{
    public delegate void BHandler(int param1);
    public void AcceptHandler(int param2, int param3, int param4, BHandler handler);
    public void InvokeHandler(int forParam1);

    // the rest is same
}

EDIT: “the rest” of the code is exact same, except calls to the delegate methods that have different signatures. Something like this:


public void StartListening()
{
   Timer timer = new Timer(CheckForChanges, null, 0, 1000);            
}

private void CheckForChanges()
{
    // pull changes, and pass different params to InvokeHandler()
}
  • 1 1 Answer
  • 3 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-05-22T01:10:56+00:00Added an answer on May 22, 2026 at 1:10 am

    Why not set it up like this:
    Edit: I’ve updated to include the methods from your edit.

    public abstract class AbstractBase {
        // "the rest"
        public void StartListening() {
            Timer timer = new Timer(CheckForChanges, null, 0, 1000);            
        }
        protected abstract void CheckForChanges();
    }
    
    public class A : AbstractBase {
        public delegate void AHandler(string param1, string param2);
        public void AcceptHandler(string param3, AHandler handler);
        public void InvokeHandler(string forParam1, string forParam2);
        protected override void CheckForChanges() {
            //Do stuff for this version of the class
        }
    }
    
    public class B : AbstractBase {
        public delegate void BHandler(int param1);
        public void AcceptHandler(int param2, int param3, int param4, BHandler handler);
        public void InvokeHandler(int forParam1);
        protected override void CheckForChanges() {
            //Do stuff for this version of the class
        }
    }
    

    This way, you’ll have all your code that is the same in a single class, and then the individual classes A and B can have whatever form of the methods you need.

    Or are you looking for a way to invoke the delegates generically irrespective of which class?

    ie. Something like:

    AbstractBase ab = new A();
    ab.InvokeDelegate();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two functions that are very similar and i'd like to make a
I have two images that are very similar. So for example suppose I drew
I have two extension methods that are very similar. I'd like to remove the
I have two very similar pieces of ASP.NET code that send a file in
I have two controls. The XAML's are big and very similar. One difference is
I have two pieces of code which are very similar in that they register
I have two very simple, identical UITableViews in my app that are populated with
I have two very similar tables in our database, and I need to write
I have two very similar .aspx pages. Both of them contain a DropDownList control.
I have two very similar methods: public IQueryable<User> Find(Func<User, bool> exp) { return db.Users.Where(exp);

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.