I have a similar problem with the message Accessing the static property of a child in the parent method . The preferred answer indicates that the design of the classes is faulty and additional information is needed to discuss the problem.
Here is the situation that I want to discuss with you.
I want to implement some data types, such as length, mass, current, ... There must be an implicit cast to instantiate from a given string. For example, the example โ1.5 mโ should give the same thing as โ150 cmโ, or โ20 inchesโ should be processed correctly.
To be able to convert between different units, I need quantitative conversion constants. My idea was to create an abstract base class with some static translation methods. To do their job, they must use a classically defined static dictionary. Therefore, consider an example.
public class PhysicalQuantities { protected static Dictionary<string, double> myConvertableUnits; public static double getConversionFactorToSI(String baseUnit_in) { return myConvertableUnits[baseUnit_in]; } } public class Length : PhysicalQuantities { protected static Dictionary<string, double> myConvertableUnits = new Dictionary<string, double>() { { "in", 0.0254 }, { "ft", 0.3048 } }; } class Program { static void Main(string[] args) { Length.getConversionFactorToSI("in"); } }
I think this makes for quite intuitive use and keeps the code compact and fully readable and extensible. But, of course, I ran into the same problems that the link mentions.
Now my question is: how can I avoid these design issues?
source share