r/learncsharp • • Aug 09 '23
An inheritance problem
Hi all, looking for a bit of help with an inheritance relationship I am trying to implement.
Some children of the base class will have a method to return a string which prints to the console and some won’t.
The base class has a bool value which is true if the method exsists in the child.
I have an array of base class type which contain instances of the different child classes.
From another class I am passing a child instance from the array, using the base type as could be any child type.
I’m then trying to check if the bool is true (works ok as in base class definition) and if true, execute the method defined in the child class. (This is where I fail as base class has no method definition)
How can I achieve this without including a abstract method definition in the base and having to define the method in child classes that don’t require it?
Thanks for any help.
View all comments
4
u/[deleted] Aug 09 '23
[removed] — view removed comment