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 8558519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:49:50+00:00 2026-06-11T15:49:50+00:00

Suppose I have a number of methods with different signatures. Based on some external

  • 0

Suppose I have a number of methods with different signatures. Based on some external code all this methods might be called. You might think of them as some event handlers.

Now I should have two implementations as so:

  • Each implementation really handles only part of all possible events.
  • Each implementation simply does nothing for all events it do not want to / can not handle.

I could of course declare an interface for all possible handlers but then I will have to create empty methods (handlers) in each implementation. Even for those events I do not want to / can not process.

I am thinking about doing something like the following:

abstract class Base
{
    public virtual void First(int i, double d) { /* no implementation */ }
    public virtual void Second(double d) { /* no implementation */ }
    public virtual void Third(string s, int i) { /* no implementation */ }
}

class Child : Base
{
    public override void First(int i, double d) { /* implementation */ }
    public override void Second(double d) { /* implementation */ }
}

class AnotherChild : Base
{
    public override void Second(double d) { /* implementation */ }
    public override void Third(string s, int i) { /* implementation */ }
}

This approach forces me to create empty implementations for all possible handlers in the base abstract class.

Could you recommend something better? An approach that doesn’t require to produce large number of empty methods?

I am using C# 2.0 and can’t use newer version of the language for this task.

  • 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-11T15:49:51+00:00Added an answer on June 11, 2026 at 3:49 pm

    I agree with @usr – I don’t see a problem with the empty functions. If you want to call a function, then it must exist. If it should do nothing in some cases, then that function should be empty. A base class with empty functions, versus an interface requiring the implementation of the same empty function over and over, seems like a very good idea.

    If you are looking for an alternative, you could consider the Chain of Responsibility design pattern. Rather than calling a specific function, you could call a general function and then parameterize the desired behavior. You could then chain objects together (different chains in different situations) and give them all a chance to handle the behavior. If none of them handle it, then nothing happens.

    This would work very well in some scenarios, but it’s more complicated to implement then the very simple and elegant base class approach. Be careful not to over-engineer.

    EXAMPLE
    Here’s an example of implementing a chain of command, based on the example you gave in your question:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication2 {
    
        interface ICommand {
            bool Execute( string action, params object[] parameters );
        }
    
        class Program {
            static void Main( string[] args ) {
    
                CommandChain l_chain1 = new CommandChain( new FirstCommand(), new SecondCommand() );
                CommandChain l_chain2 = new CommandChain( new SecondCommand(), new ThirdCommand() );
    
                // Chain 1
    
                if ( l_chain1.Execute( "first", (int) 1, (double) 1.1 ) )
                    Console.WriteLine( "Chain 1 executed First" );
                else
                    Console.WriteLine( "Chain 1 did not execute First" );
    
                if ( l_chain1.Execute( "second", (double) 1.2 ) )
                    Console.WriteLine( "Chain 1 executed Second" );
                else
                    Console.WriteLine( "Chain 1 did not execute Second" );
    
                if ( l_chain1.Execute( "third", "4", (int) 3 ) )
                    Console.WriteLine( "Chain 1 executed Third" );
                else
                    Console.WriteLine( "Chain 1 did not execute Third" );
    
                // Chain 2
    
                if ( l_chain2.Execute( "first", (int) 1, (double) 1.1 ) )
                    Console.WriteLine( "Chain 2 executed First" );
                else
                    Console.WriteLine( "Chain 2 did not execute First" );
    
                if ( l_chain2.Execute( "second", (double) 1.2 ) )
                    Console.WriteLine( "Chain 2 executed Second" );
                else
                    Console.WriteLine( "Chain 2 did not execute Second" );
    
                if ( l_chain2.Execute( "third", "4", (int) 3 ) )
                    Console.WriteLine( "Chain 2 executed Third" );
                else
                    Console.WriteLine( "Chain 2 did not execute Third" );
    
                Console.ReadKey( true );
    
            }
        }
    
        class CommandChain {
    
            private ICommand[] _commands;
    
            public CommandChain( params ICommand[] commands ) {
                _commands = commands;
            }
    
            public bool Execute( string action, params object[] parameters ) {
                foreach ( ICommand l_command in _commands ) {
                    if ( l_command.Execute( action, parameters ) )
                        return true;
                }
                return false;
            }
    
        }
    
        class FirstCommand : ICommand {
            public bool Execute( string action, params object[] parameters ) {
                if ( action == "first" &&
                    parameters.Length == 2 &&
                    parameters[0].GetType() == typeof( int ) &&
                    parameters[1].GetType() == typeof( double ) ) {
    
                    int i = (int) parameters[0];
                    double d = (double) parameters[1];
    
                    // do something
    
                    return true;
                } else
                    return false;
            }
        }
    
        class SecondCommand : ICommand {
            public bool Execute( string action, params object[] parameters ) {
                if ( action == "second" &&
                    parameters.Length == 1 &&
                    parameters[0].GetType() == typeof( double ) ) {
    
                    double d = (double) parameters[0];
    
                    // do something
    
                    return true;
                } else
                    return false;
            }
        }
    
        class ThirdCommand : ICommand {
            public bool Execute( string action, params object[] parameters ) {
                if ( action == "third" &&
                    parameters.Length == 2 &&
                    parameters[0].GetType() == typeof( string ) &&
                    parameters[1].GetType() == typeof( int ) ) {
    
                    string s = (string) parameters[0];
                    int i = (int) parameters[1];
    
                    // do something
    
                    return true;
                } else
                    return false;
            }
        }
    
    }
    

    (Please note that this example does not follow every programming best practice – I would not recommend implementing EXACTLY this code. For example, the action parameter would probably be better as an enum than a string, and returning some kind of CommandResult rather than a boolean. Use it for inspiration only.)

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

Sidebar

Related Questions

suppose I have this string: some striinnngggg <a href=something/some_number>linkk</a> soooo <a href=someotherthing/not_number>asdfsadf</a> I want
Let's suppose I have n arrays, where n is a variable (some number greater
Suppose I have a number of related classes that all have a method like
Here's something I've been thinking about: suppose you have a number, x, that can
Suppose I have a VC++ project containing number of Source (.cpp) files (e.g. 5),
Suppose I have a structure in C++ containing a name and a number, e.g.
Suppose I have two tables, - emp(empId number(1),empName varchar2(50)) and - manager(manId number(5),managerName varchar2(100))
Suppose I have a table Item (Id int Primary Key, Number INT) having records
Suppose that we have following tables create table Employee( 2 EMPNO NUMBER(3), 3 ENAME
I have a number of thumbnails on my website, that are supposed to all

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.