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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:53:27+00:00 2026-06-15T12:53:27+00:00

Let’s assume the following class structure with base class BC and 2 derived classes

  • 0

Let’s assume the following class structure with base class BC and 2 derived classes DC_A and DC_B;
Furthermore there is a class XY with a methode goo() with a parameter of type BC and other methodes

// base class
public class BC
{
    public virtual void foo();
}

// derived class A
public class DC_A : BC
{
    public override void  foo() {}
}

// derived class B
public class DC_B : BC
{
    public override void  foo() {}
}


public class XY
{
    public void goo(BC o)
    {
        // perfectly fine using polymorphism; no Ifs' no casting, OOP at its best ;-)
        o.foo();

        // but what to do here?
        if ( (o as DC_A) != null )
        {
            doX(o as DC_A);
        }
        else if ((o as DC_B) != null)
        {
            doY(o as DC_B);
        }
    }

    private void doX(DC_A o) {}
    private void doY(DC_B o) {}
}

In ‘Is passing around value objects using polymorphism a bad practice?‘ the Visitor pattern is proposed;

The problem of the If cascade and the casting is moved (not eliminated) into the abstract base class.

Are there better solutions to completely avoid the if’s?

Its no option for me (in this example) to move the functionality from doX/doY to class DC_A/DC_B

Your advice is much appreciated.

Edit:
The background of this question is a C# / WinForms application with a form to manage a “test rule” consisting of differenct sub entities like TestSpec with a collection of measurement types, test limits, and so on (my DC_A, DC_B classes) all derived from EntityBase (=BC from above)
The form sends an event to the controller that a sub entity has changed;
simplified PropertiesChanged(EntityBase o)
The controller calls the corresponding methode in the model class (methode goo in class XY from above) which is now responsible to do the businesslogic which is not only persisting the changed sub entity test limit but also e.g. creating a new test limit revision object, increasing a test spec revision, etc..
Maybe in this way
the generic approach of having a methode like PropertiesChanged(EntityBase o) should be changed to more specifing events from the form and specific event handler in controller and model class to handle “TestLimitChanged” etc.

But this special case has led me to the more generic or “philosophical” question about polymorphism at all 😉

  • 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-15T12:53:27+00:00Added an answer on June 15, 2026 at 12:53 pm

    If you are using .NET 4 there is a possibility with overloading and dynamic type, maybe that is an alternativ for you.

    class Program
    {
        static void Main(string[] args)
        {
            DC_A dca = new DC_A();
            DC_B dcb = new DC_B();
            XY xy = new XY();
            xy.goo(dca);
            xy.goo(dcb);
        }
    }
    
    // base class
    public abstract class BC
    {
        public abstract void foo();
    }
    
    // derived class A
    public class DC_A : BC
    {
        public override void foo() { }
    }
    
    // derived class B
    public class DC_B : BC
    {
        public override void foo() { }
    }
    
    
    public class XY
    {
        //public void goo<T>(T o) where T : BC
        //{
        //    //dynamic dyn = Convert.ChangeType(o, o.GetType());
        //    //dynamic dyn = o;
        //    //gooi(dyn);
        //    gooi((dynamic)o);
        //}
    
        // http://smellegantcode.wordpress.com/2008/11/04/dynamic-typing-in-c-4-and-duck-generics/
        public void goo<T>(T o) where T : BC
        {
            gooi((dynamic)o);
        }
    
        private void gooi(DC_A o)
        {
            o.foo();
            doX(o);
        }
    
        private void gooi(DC_B o)
        {
            o.foo();
            doY(o);
        }
    
        private void gooi(BC o)
        {
            o.foo();
        }
    
        private void doX(DC_A o) { }
        private void doY(DC_B o) { }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following classes : public class MyProductCode { private String
Let's say I have the following entity: public class Store { public List<Product> Products
Let's say I have an abstract parent class called shape, and that there are
Let's say that I have classes like this: public class Parent { public int
Let's say I have an empty class with a virtual function: class Base {
Let's assume I have the following typedef: typedef void (^myBlock)(id); And I have some
Let me explain best with an example. Say you have node class that can
Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let's assume that a user votes for some movies in a scale of 1
let's say I have the following string: string s = A B C D

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.