I am writing a class with member variables. I want to define default values for these fields and have to ability to override them with custom set values. I want to create some sort of class or struct in order to hold the data for these variables. I want someone using the class to have the ability to define all of the variables, or if they don't then the fields will be set to defaults (which I would define). Not sure of cleanest way to do this. This is what I have in mind but I'm not sure if I can do better:
public class ReportPageParams
{
public float Width { get; private set; }
public float Height { get; private set; }
public float LeftMargin { get; private set; }
public float RightMargin { get; private set; }
public float TopMargin { get; private set; }
public float BottomMargin { get; private set; }
//Constructor
ReportPageParams(float pWidth, pHeight)
{
Width = 52f
Height = 52f
//...
}
}
public class ReportPage
{
//same fields as ReportPageParams plus some more
private float _width, _height;
private float _leftMargin;
private float _rightMargin;
//...
ReportPage(ReportPageParams pCustomParams = null)
{
if (pCustomParams != null)
{
_width = pCustomParams.Width
_height = pCustomParams.Height
//...
}
else
{
//set fields to default values
}
}
}
Quick and dirty way: make your properties protected set
, then create a class that is derived from your defaults. Only your base and derived classes will be able to make changes due to the properties being protected
.
public class ReportPageParams
{
public float Width { get; protected set; }
public float Height { get; protected set; }
public float LeftMargin { get; protected set; }
public float RightMargin { get; protected set; }
public float TopMargin { get; protected set; }
public float BottomMargin { get; protected set; }
//Constructor
public ReportPageParams(float pWidth, float pHeight)
{
Width = 52f
Height = 52f
//...
}
}
public class ReportPageParamsWithLargeLeftMargin : ReportPageParams
{
//Constructor
public ReportPageParamsWithLargeLeftMargin(float pWidth, float pHeight)
: base(pWidth, pHeight)
{
LeftMargin = 100;
}
}
You'll find whatever values you set in the constructor for ReportPageParams
will also appear in ReportPageParamsWithLargeLeftMargin
. Because constructors are executed in order from the class closest to object
to more specific classes, your changed defaults will be applied.
Alternatively, you can make the properties virtual
and derive a class that overrides them with override
. This puts you in charge of handling the property yourself.