我有这样一个代码,可以让它变得更好(modbus_master.SetValue("x1",Convert.ToInt32(resipeDosingsi.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);
}
}发布于 2011-04-07 18:03:18
嗯,只要你对待他们的方式没有区别:
public List<RecipeDosings> resipeDosings = new List<RecipeDosings>();
for (int i = 0; i < resipeDosings.Count; i++)
{
if (resipeDosings[i].Bunker.StartsWith("Bunker "))
{
modbus_master.SetValue("x1", Convert.ToInt32(resipeDosings[i].Massa) * 10, 1);
}
}https://stackoverflow.com/questions/5579247
复制相似问题