Well, initially I had several constants (e.g. MAX_SPEED) with different values ββin each of the derived classes. The idea was to use these values ββin some methods of the base class. This is when I realized that I cannot do this with constants, so I created read-only properties.
I need a method to assign these values ββto private fields at the time of creation, preferably in the base class . But first, I have to evaluate the initial values ββin the derived classes. Since these are properties, I could not find a way to initialize them in the definition, so the only way to do this is in derived constructors.
What the problem is: the values ββare initialized after they are assigned to private fields in the base class. The solution I'm leaving with is to create a virtual method and assign there.
Is there a way to call the base constructor from the derived class so that the code from the derived constructor is called first?
class BaseClass { public BaseClass() { System.Console.WriteLine("This should be shown after"); } } class DerivedClass : BaseClass { public DerivedClass() : base() { System.Console.WriteLine("This should be shown first"); } }
Of course, in the example, this will work the other way around. Is there a solution?
source share