我有这样一个代码,可以让它变得更好(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 17:59:41
Switch语句删除所有if语句-
switch (resipeDosings[i].Bunker)
{
case "Bunker 1":
// code here
break;
case "Bunker 2":
// code here
break;
// repeat case statements...
default:
// this is the final 'else' code - if nothing matches
}然而,有两件事是显而易见的:
时都修改程序
构建查找表的最简单方法是使用Dictionary<>。
Dictionary<string, int> bunkerLut = new Dictionary<string, int>();
bunkerLut["Bunker 1"] = 10;
bunkerLut["Bunker 2"] = 11;
bunkerLut["Bunker 3"] = 12;
// and so on... I'm assuming there should be a value that's different for each bunker? I'm also assuming it's an int然后,您可以向上查找,如下所示(假设10是更改的值):
int result = bunkerLut[resipeDosings[i].Bunker];
modbus_master.SetValue("x1", Convert.ToInt32(resipeDosings[i].Massa) * result, 1);其他选项是将值存储在配置文件或数据库中。
发布于 2011-04-07 18:03:47
您可以执行以下操作:
for (int i = 0; i < resipeDosings.Count; i++)
{
switch(resipeDosings[i].Bunker)
{
case "Bunker 1":
case "Bunker 2":
case "Bunker 3":
case "Bunker 4":
case "Bunker 5":
case "Bunker 6":
case "Bunker 7":
case "Bunker 8":
case "Bunker 9":
case "Bunker 10":
modbus_master.SetValue("x1", Convert.ToInt32(resipeDosings[i].Massa) * 10, 1);
break;
default:
break;
}
}发布于 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
复制相似问题