I have some code to handle integer, double, Boolean, and string settings. Right now, each setting variant extends an abstract class, where the setting value is stored as an object
.
Is there any way to avoid this? What I'd like to do is have IntSetting
objects return their Value
as an integer, DoubleSetting
objects return their Value
as a double and so on and so forth.
Here is the code referenced above:
public abstract class ModelSetting
{
public object Value { get; set; }
public abstract string ToString();
public ModelSetting(object value)
{
Value = value;
}
}
public class IntSetting : ModelSetting
{
public IntSetting(int value)
: base(value)
{
}
public override string ToString()
{
return (bool)Value ? "1" : "0";
}
}
public class DoubleSetting : ModelSetting
{
public DoubleSetting(double value)
: base(value)
{
}
public override string ToString()
{
return ((double)Value).ToString("%g");
}
}
IntSetting => (bool)Value ? "1" : "0";
and would be off topic here. \$\endgroup\$