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

Your design is very likely to be flawed. You should consider making the behavior a method of BaseClass and override it in each derived class. You shouldn't check for the actual type of the object That is ExternalClass should just call a method declared in BaseClass regardless of the actual type.

Since the method is overriden by derived classes, the appropriate implementation will be called That said, to check if an object is an instance of a type or its derived classes, you can use the is operator: if (obj is DerivedA) // C# If TypeOf obj Is DerivedA Then ' // VB If you want to check if the object is an instance of a specific type (and not its derived types): if (obj.GetType() == typeof(DerivedA)) // C# If obj.GetType() Is GetType(DerivedA) Then ' // VB.

Your design is very likely to be flawed. You should consider making the behavior a method of BaseClass and override it in each derived class. You shouldn't check for the actual type of the object.

That is, ExternalClass should just call a method declared in BaseClass regardless of the actual type. Since the method is overriden by derived classes, the appropriate implementation will be called. That said, to check if an object is an instance of a type or its derived classes, you can use the is operator: if (obj is DerivedA) // C# If TypeOf obj Is DerivedA Then ' // VB If you want to check if the object is an instance of a specific type (and not its derived types): if (obj.GetType() == typeof(DerivedA)) // C# If obj.GetType() Is GetType(DerivedA) Then ' // VB.

Hm. I see. – Jason Aug 29 '09 at 0:04 10 +1 solid advice.

If you ever find yourself doing a switch on a class type in an inheritance tree, you're probably doing something wrong. – womp Aug 29 '09 at 0:07 3 thanks for letting me know i'm going about it wrong. This is helpful for sure.

– Jason Aug 29 '09 at 0:11 @womp - what if I am in a UserControl that has the BaseClass as a member and must instantiate it via only the ID passed to it as a parameter. How do I know which derived class to instantiate unless I also pass along something like an enumerated value to tell it which one? – Jason Aug 29 '09 at 0:35 3 Jason: Use a factory method which uses a prepopulated Dictionary> to lookup the instantiation function of each derived class and call it.

That's really a separate thing. You are not switching on "type" of an object. You're switching on the ID passed and "creating" the class.In the currect question, you have an object that's supposed to behave differently based on the type, which is what polymorphism does.

– Mehrdad Afshari Aug 29 '09 at 0:40.

This is precisely what polymorphism is designed to let you do, frequently riding under the tagline "select is harmful. " A good rule of thumb: you should never have to use a select statement to differentiate between different types of objects. Create a method on BaseClass, even if it's abstract and does nothing.

This communicates (to humans and to compilers) that all subclasses of BaseClass need to implement that operation. Then implement it appropriately in DerivedA, DerivedB, and DerivedC. This way, simply having a variable declared as type BaseClass entitles you to call that method.It's up to ASP.

NET to work out which specific implementation is appropriate based on the type of object you actually end up having.

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 real-world ExternalClass would be non-static, of course. Alternately you can use the following to share behavior: public class BaseClass { public virtual void SomeAction() { Console.

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

While I totally agree with Mehrdad, here's how you can check for the type of an object: public MyMethod(BaseClass obj) { if (obj is DerivedA) { ... } if (obj is DerivedB) { ... } }.

To be picky, this tests whether an object can be cast to DerivedA, not whether it is DerivedA. If DerivedA had a child named DerivedAPrime, it would pass the first test, which may or may not be a good thing. – Steven Sudit Aug 29 '09 at 0:49.

You do not need to check types to do what you want to do. You should look at Visitor pattern. You can find all information about it in GoF book or at dofactory.com, but let me explain my point: Your external class will implement IVisitor interface that will have methods DoDerivedA(), DoDerivedB and DoDerivedC.

After that you should add to BaseClass virtual function that will use your external class: public virtual void DoExternal(IVisitor v){} DerivedA will override this method like that: v.DoDerivedA(); After that you`ll have something like that in your External: AcceptBaseByExternal(BaseClass derivedInstance) { derived. DoExternal(this); } This will do anything you want according to the actual class type. All you need is create a specific method for every derived class.

When I wrote it I also thought that you could create one method in your ExternalClass instead of single method for single derived class and parametrize it with some parameter. E.g. Implement virtual function in BaseClass that returns enum and every derived should override that enum so that ExternalClass know what code it should execute.

IF you're looking to differentiate explicitly between the parent/derived class, IMO, that is an indication to review the design. Used correctly, the derived class should be directly substitutable. Use a virtual function instead .

Switch has its place in OOP - where an object can change its state and the current action is based on its present state. Most of the time, switch is misused. If you ever find you are using switch on a value which remains constant within your object then you should probably be using polymorphism.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions