I have this little OO problem..
abstract class MyAbstractBaseClass
{
MyHelperClassBase myHelperBaseClass;
protected method foo()
{
//QUESTION....... : when I am here I want to do this...
myHelperBaseClass = new MyHelperSubClass();
//OR...
myHelperBaseClass = new MyOtherHelperSubClass();
//HOWEVER ITS COMPLETELY CONDITIONAL ON THE BASE CLASS
//HOW DO I MAKE SURE THE CORRECT TYPE IS INSTANTIATED DEPENDANT ON
//THE BASE CLASS WITHOUT USING AN 'if' STATEMENT?
}
}
class MySubClass : MyAbstractClass {}
class MyOtherSubClass : MyAbstractClass {}
/////////////
abstract class MyHelperClassBase {}
class MyHelperSubClass : MyHelperClassBase, IMyHelper {}
class MyOtherHelperSubClass : MyHelperClassBase, IMyHelper {}
I want to be able to have a the type basically passed up but cant think of a slick way to do this. Should I just stuck an abstract property on the base class?
The older OO way of solving this (without generics) involves a virtual or abstract method on the base that the derived classes must implement. This is pretty common in parallel hierarchies like your example:
Generics come in very handy, but sometimes it’s good to know this way, too.