What is the best way to differentiate derived base class classes?

I have a base class BaseClass

and derived classes DerivedA

, DerivedB

and DerivedC

that inherit BaseClass

.

I have another class ExternalClass

with a method that takes a type parameter BaseClass

but is actually passed in a derived class. What is the best way to differentiate between these classes in ExternalClass

case I wanted to perform a different action based on which derived class it got?

I thought to do it Select

, but I don't know exactly how to do it.

+2


source to share


7 replies


Your design is likely to be flawed. You should consider making the behavior a method BaseClass

and overriding it in each derived class. You don't have to check the actual type of the object.

That is, it ExternalClass

should simply call the method declared in BaseClass

, regardless of the actual type. Since the method is being overridden by derived classes, the corresponding implementation will be called.

However, to check if an object is an instance of a type or its derived classes, you can use the operator is

:



if (obj is DerivedA) // C#
If TypeOf obj Is DerivedA Then ' // VB

      

If you want to check if an object is an instance of a certain type (and not its derived types):

if (obj.GetType() == typeof(DerivedA)) // C#
If obj.GetType() Is GetType(DerivedA) Then ' // VB

      

+18


source


This is exactly what polymorphism is meant to be done, often skimming under the slogan " select

harmful". A good rule of thumb: you don't need to use a statement select

to differentiate between different types of objects.

Create a method on BaseClass

, even if it abstract

doesn't do anything. This binds (for humans and compilers) that all subclasses BaseClass

must implement this operation. Then implement it appropriately in DerivedA

, DerivedB

and DerivedC

.



Thus, simply having a variable declared as a type BaseClass

gives you the right to call that method. It is up to ASP.NET to determine which specific implementation is appropriate depending on the type of object you actually have.

+2


source


Here's a very simple example:

using System;

public abstract class BaseClass
{
    public abstract void SomeAction();
}

public class DerivedA : BaseClass
{
    public override void SomeAction()
    {
        Console.WriteLine("A!");
    }
}

public class DerivedB : BaseClass
{
    public override void SomeAction()
    {
        Console.WriteLine("B!");
    }
}

public class ExternalClass
{
    public static void Main()
    {
        DoIt(new DerivedA());
        DoIt(new DerivedB());

        Console.ReadLine();
    }

    public static void DoIt(BaseClass baseClass)
    {
        baseClass.SomeAction();
    }
}

      

Presumably your external world of ExternalClass would be non-stationary, of course.

Alternatively, you can use the following:

public class BaseClass
{
    public virtual void SomeAction()
    {
        Console.WriteLine("Base!");
    }
}

public class DerivedA : BaseClass
{
    public override void SomeAction()
    {
        Console.WriteLine("A!");
        base.SomeAction();
    }
}

      

+2


source


While I completely agree with Mehrdad, you can check the object type here:

public MyMethod(BaseClass obj)
{
  if (obj is DerivedA) { ... }
  if (obj is DerivedB) { ... }
}

      

0


source


You don't need type checking to do what you want to do. You should look at the visitor template. You can find all the information on this in the GoF book or at www.dofactory.com, but let me explain my point:

The outer class will implement the IVisitor interface, which will have DoDerivedA (), DoDerivedB, and DoDerivedC methods. After that, you must add to the BaseClass virtual function that will use your outer class:

public virtual void DoExternal(IVisitor v){}

      

DerivedA will override this method as follows:

v.DoDerivedA();

      

After that, you will have something like this in your External:

AcceptBaseByExternal(BaseClass derivedInstance)
{
  derived.DoExternal(this);
}

      

This will do whatever you want according to the type of the concrete class. All you need to do is create a specific method for each derived class.

When I wrote this, I also thought that you could create one method in your ExternalClass instead of one method for one derived class and parameterize it with some parameter. For example. implement a virtual function in BaseClass that returns an enum, and each derivative must override this enum so that ExternalClass knows what code it should execute.

0


source


IF you want to explicitly differentiate between parent / derived class, IMO that is a feature for design review. Used correctly, a derived class must be directly replaceable.

Use a virtual function instead.

0


source


Switch

has its place in OOP, where an object can change its state, and the current action is based on its current state.

In most cases, the switch is not used correctly. If you ever find you are using a switch on a value that remains constant in your object, then you should probably use polymorphism.

0


source







All Articles