Reputation: 679
My problem here is that I would like to pass an object to a derived class, but it must be done before the base class constructor, since the base class will immediately call the derived class's Start()
method that uses the object.
Here's an excerpt from the base class, (renamed from BarcodeScanner for convenience).
public abstract class MyBase
{
public MyBase()
{
if (Initialize())
this.Start();
}
public abstract bool Initialize();
public abstract void Start();
}
Here's the derived class that I'm creating.
class MyDerived : MyBase
{
private string sampleObject;
public MyDerived (string initObject)
{
sampleObject = initObject;
}
public override bool Initialize()
{
return GetDevice();
}
public override void Start()
{
Console.WriteLine("Processing " + sampleObject.ToString());
}
}
I doubt you can make C# execute a derived constructor before the base constructor; so I'm really just looking for a solution to pass an object to the derived class before the object is used.
I've gotten around this by putting the Initialize/Start if block inside the MyDerived
constructor. However, there are other classes deriving from the base class; so I ended up having to repeat this block of Initialize/Start code in every derived class. I'd like to see an alternative to modifying the base class.
Upvotes: 21
Views: 27961
Reputation: 55
Sorry for adding to an old thread, but maybe someone is interested in another answer. I found an (IMO) neat way to handle logic doing something more than (and after) just assigning fields in class constructors where inheritance is involved here. If you just want to have if for this specific hierarchy and not use the generic solution with an interface and an extension method, you could use the same concept in one class tree like this:
public abstract class MyBase
{
protected MyBase()
{
Initialize(this) // just to illustrate this will never do anything as MyBase can never be the run time type.
}
protected bool IsInitialized { get; private set; } = false;
protected static bool Initialize<T>(T instance) where T: MyBase
{
if (instance?.GetType() == typeof(T)) // check if this is called from the constructor of instance run time type
return instance.IsInitialized || ( instance.IsInitialized = instance.Initialize() );
return false;
}
protected abstract bool Initialize();
public abstract void Start();
}
and derived:
class MyDerived : MyBase
{
private string sampleObject;
protected bool started = false;
public MyDerived (string initObject)
{
sampleObject = initObject;
if (Initialize(this)) // if this is the most derived constructor, this will run Initialize() and return whether it was successful
this.Start();// EDIT: Just to illustrate. Normally constructors should only initialize an instance and not perform operations on it (as mentioned in other answers).
}
protected override bool Initialize()
{
return GetDevice();
}
public override void Start()
{
// if Start() would be protected, we don't need the IsInitialized property and we can move this check to the constructor on the returned value of the Initialize<T>() call.
if (!IsInitialized) throw new InvalidOperationException("Initialization failed.");
// if you want to have this method exposed public, we need to check if this instance is successfully initialized from the constructor and not in started state already.
if (started) return;
Console.WriteLine("Processing " + sampleObject.ToString());
started = true;
if (!Run(sampleObject)) started = false;
}
}
Upvotes: 0
Reputation: 564441
I would rework your design so that Initialize (and potentially Start() - though I'd normally have this be a public method that's called by the user) are called after construction.
If you're making a BarcodeScanner, you could do this the first time you go to scan. Just lazy-initialize your members using the data from the derived class.
This will work around your issue, with no real change in usage from the user.
Upvotes: 1
Reputation: 29956
IMHO your design is wrong. You shouldn't start the process from within the constructor. Your consuming code should explicitly call the Start() method when required.
Upvotes: 22
Reputation: 351526
What you are trying to do is impossible in C#. A constructor in a base class must be run before the constructor of any derived class otherwise there would be potential for corrupt object state. A child object must be able to assume that its base is fully constructed and available.
Upvotes: 24