У меня есть такой код, чтобы сделать его лучше (modbus_master.SetValue ("x1", Convert.ToInt32 (resipeDosings [i] .Massa) * 10, 1); - отправка данных в контроллер)Как упростить выбор (если)
public class RecipeDosings
{
public string Product { get; set; }
public string Persent { get; set; }
public string Massa { get; set; }
public string Bunker { get; set; }
public RecipeDosings(string product, string persent, string massa, string bunker)
{
this.Product = product;
this.Persent = persent;
this.Massa = massa;
this.Bunker = bunker;
}
}
public List<RecipeDosings> resipeDosings = new List<RecipeDosings>();
for (int i = 0; i < resipeDosings.Count; i++)
{
if (resipeDosings[i].Bunker == "Bunker 1")
{
modbus_master.SetValue("x1", Convert.ToInt32(resipeDosings[i].Massa) * 10, 1);
}
if (resipeDosings[i].Bunker == "Bunker 2")
{
modbus_master.SetValue("x1", Convert.ToInt32(resipeDosings[i].Massa) * 10, 1);
}
if (resipeDosings[i].Bunker == "Bunker 3")
{
modbus_master.SetValue("x1", Convert.ToInt32(resipeDosings[i].Massa) * 10, 1);
}
if (resipeDosings[i].Bunker == "Bunker 4")
{
modbus_master.SetValue("x1", Convert.ToInt32(resipeDosings[i].Massa) * 10, 1);
}
if (resipeDosings[i].Bunker == "Bunker 5")
{
modbus_master.SetValue("x1", Convert.ToInt32(resipeDosings[i].Massa) * 10, 1);
}
if (resipeDosings[i].Bunker == "Bunker 6")
{
modbus_master.SetValue("x1", Convert.ToInt32(resipeDosings[i].Massa) * 10, 1);
}
if (resipeDosings[i].Bunker == "Bunker 7")
{
modbus_master.SetValue("x1", Convert.ToInt32(resipeDosings[i].Massa) * 10, 1);
}
if (resipeDosings[i].Bunker == "Bunker 8")
{
modbus_master.SetValue("x1", Convert.ToInt32(resipeDosings[i].Massa) * 10, 1);
}
if (resipeDosings[i].Bunker == "Bunker 9")
{
modbus_master.SetValue("x1", Convert.ToInt32(resipeDosings[i].Massa) * 10, 1);
}
}
Есть много повторений. Есть ли лучший способ справиться с этим без кодирования «числа» в названии (или что-то еще)? –