I have an abstract baseclass with some protected members, but no abstract members:
internal abstract class BaseClass
{
protected readonly string Var1;
protected readonly string Var2;
protected BaseClass(string var1, string var2)
{
Var1 = var1;
Var2 = var2;
}
protected int DoSomething1(string s)
{
}
}
And some classes that implements this baseclass and have some specific methods different for each one:
internal sealed class Child1:BaseClass
{
internal Child1(string var1, string var2)
: base(var1, var2)
{
}
internal bool DomeSomethingSpecificForChild1(int i)
{
}
}
Now I have another class where I would like to have a variable of type BaseClass and depending of what I need, intantiate it with a child type.
But this isn’t working since the specific methods are not known.
public class MyClass
{
private BaseClass myBaseClass;
public bool DomeSomethingSpecific(int i)
{
myBaseClass = new Child1("a","b");
myBaseClass.DomeSomethingSpecificForChild1(i);
}
}
Is there a way I can accomplish what I want?
Or do I have to declare my variable in the inner scope of my method in MyClass?
public class MyClass
{
private BaseClass myBaseClass;
public bool DomeSomethingSpecific(int i)
{
var child1 = new Child1("a","b");
myBaseClass.DomeSomethingSpecificForChild1(i);
}
}
but this way I have no certainty that the class implements BaseClass.
An interface is not an option since I want it to stay internal, not public,
and I don’t want the baseclass to be instantiated, so I thought that abstract would be a good choice here..
1 Answer