I have a 3 classes, ParentClass
,ClassA
,ClassB
. Both ClassA
and ClassB
are subclasses of ParentClass
. I wanna try to create objects of type ClassA
or ClassB
using some kind of enumeration to identify a type, and then instantiate the object cast as the parent type. How can I do that dynamically? Please take a look at the code below, and the parts that say //what do I put here?
. Thanks for reading!
enum ClassType
{
ClassA,
ClassB
};
public abstract class ParentClass
{
public ParentClass()
{
//....
}
public static ParentClass GetNewObjectOfType(ClassType type)
{
switch(type)
{
case ClassType.ClassA:
//What do I put here?
break;
case ClassType.ClassB:
//What do I put here?
break;
}
return null;
}
}
public class ClassA:ParentClass
{
//....
}
public class ClassB:ParentClass
{
//.......
}