I have the following problem:
The base class expects to receive some data but the data is initialized by the derived class constructor which in C# is called after the base constructor was called.
Context / What I'm trying to solve:
Let's call the base class Track
, its role is to build a mesh that represents a track for a video game.
The derived classes, e.g. Track1
each fetch track data from a particular file format, with significant differences that forbids implementing the whole code in base class Track
.
The main job of Track
is to abstract the data incoming from derived classes and for this it has abstract members that derived classes have to implement, e.g. int GetVertexCount
, Vector3 GetVertex(int)
.
Think more of less of it being an IPicture
interface that can load from different formats, e.g. BMP, JPEG, and return the whole thing as an abstraction.
The problem I am facing:
In C#, base class constructors are called before derived class constructor, but I must initialize something in the derived class constructor that in turn I must pass to the base class constructor. And while I'm on it, I would like to have members to be immutable, i.e. readonly
.
Question:
How can I run some code in derived class constructor first, so I can pass the result to the base constructor ?
Answer:
Following @Kit answer here's how I ended up doing and it's just fine:
Ironically, it ended up being a C-like API :)