I am designing a program that handles many types of grooves TKY, VSingle
under GrooveType
.
Each of this groove is paired with its specific parameter SimulationParam
(e.g. TKY
will pair with SimulationParamTKY
, which inherits from SimulationParam
) under the Dictionary simParams
. As such, if i want to include more types of grooves, I can add it under this dictionary.
SimulationParam
contains ProcessParam
, which has the parameters probeStartAngle
and probeStartDist
. These parameters are similar(in meaning) for both TKY
and VSingle
.
However, in the dialog, I want the user to be able to select which particular groove they want. When that particular option is selected, the respective values for probeStartAngle
and probeStartDist
is read in from their respective text box. For example, if Use TKY
is selected, the ProcessParam.probeStartAngle
value will be read from tbTKYStartAngle
.
As such, when I extend this program to even more grooves (say 10 more), under btnProcess_Click()
, a lot of code will actually be repeated.
Is there a better way to write this code to reduce the repetitions? Assuming the requirement is that the textboxes for each groove cannot be merged into a single one. (i.e. meaning i cannot read from the same textbox for say ProcessParam.probeStartAngle
)
public partial class GrooveDialog : Form
{
public enum GrooveType : int
{
TKY = 0,
VSingle = 1,
}
Dictionary<GrooveType, SimulationParam> simParams = new Dictionary<GrooveType, SimulationParam>();
public GrooveDialog()
{
simParams.Add(GrooveType.TKY, new SimulationParamTKY());
simParams.Add(GrooveType.VSingle, new SimulationParamVSingle());
}
private void btnProcess_Click(object sender, EventArgs e)
{
if (rbUseTKY.Checked)
{
var simParamTKY = (SimulationParamTKY)simParams[GrooveType.TKY];
simParamTKY.processParam.probeStartAngle = Convert.ToDouble(tbTKYAngle.Text);
simParamTKY.processParam.probeStartDist = Convert.ToDouble(tbTKYDist.Text);
}
else if (rbUseVSingle.Checked)
{
var simParamVSingle = (SimulationParamVSingle)simParams[GrooveType.VSingle];
simParamVSingle.processParam.probeStartAngle = Convert.ToDouble(tbVSingleAngle.Text);
simParamVSingle.processParam.probeStartDist = Convert.ToDouble(tbVSingleDist.Text);
}
}
public class ProcessParam
{
public ProcessParam() { }
public double probeStartAngle { get; set; }
public double probeStartDist { get; set; }
}
public class SimulationParam
{
public ProcessParam processParam { get; set; }
}
public class SimulationParamTKY : SimulationParam
{
public double alpha, theta;
public SimulationParamTKY()
{
this.processParam = new ProcessParam();
}
public SimulationParamTKY(ProcessParam parameters)
: this()
{
this.processParam = parameters;
}
}
public class SimulationParamVSingle : SimulationParam
{
public double deta, eta;
public SimulationParamVSingle()
{
this.processParam = new ProcessParam();
}
public SimulationParamVSingle(ProcessParam parameters)
: this()
{
this.processParam = parameters;
}
}