I have two settings classes one abstract base class from which the second settings class derives
public abstract class BaseFooSettings
{
public int a;
public int b;
public float c;
}
public class DerivedBarSettings : BaseFooSettings
{
public float d;
}
I then have two classes one base class and a second class that derives from the first
public abstract class BaseFoo
{
private int a = 0;
private int b = 1;
private float c = 1.2f;
public BaseFoo() { }
public abstract BaseFooSettings WriteSettings();
public int A
{
get { return a; }
set { a = value; }
}
public int B
{
get { return b; }
set { c = value; }
}
public float C
{
get { return c; }
set { c = value; }
}
}
public class DerivedBar : BaseFoo
{
float d = 0.9f;
public DerivedBar()
: base() { }
}
With this setup it is easy to save the state of the derived class by saving its settings using the following method
public override BaseFooSettings WriteSettings()
{
DerivedBarSettings settings = new DerivedBarSettings();
settings.a = A;
settings.b = B;
settings.c = C;
settings.d = d;
return settings;
}
The problem with this approach is that all the settings must be explicitely set in the derived class and not just the local or derived class settings.
What I would like to do is something akin to
public override BaseFooSettings WriteSettings()
{
DerivedBarSettings settings = new DerivedBarSettings();
// Write base settings
WriteBaseFooSettings(ref settings);
// Write derived/local settings
settings.d = d;
return settings;
}
The base class would then look like
public abstract class BaseFoo
{
private int a = 0;
private int b = 1;
private float c = 1.2f;
public BaseFoo() { }
public abstract BaseFooSettings WriteSettings();
protected void WriteBaseFooSettings(ref BaseFooSettings settings)
{
settings.a = a;
settings.b = b;
settings.c = c;
}
public int A
{
get { return a; }
set { a = value; }
}
public int B
{
get { return b; }
set { c = value; }
}
public float C
{
get { return c; }
set { c = value; }
}
}
This approach doesn't work though as I cannot convert from 'ref DerivedBarSettings' to 'ref BaseFooSettings'.
How can I implement the new approach correctly?