首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何简化选择(if)

如何简化选择(if)
EN

Stack Overflow用户
提问于 2011-04-07 17:58:26
回答 7查看 150关注 0票数 0

我有这样一个代码,可以让它变得更好(modbus_master.SetValue("x1",Convert.ToInt32(resipeDosingsi.Massa) * 10,1);-向控制器发送数据)

代码语言:javascript
复制
 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);
            }
        }
EN

Stack Overflow用户

发布于 2011-04-07 18:03:18

嗯,只要你对待他们的方式没有区别:

代码语言:javascript
复制
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);      
    }
}
票数 2
EN
查看全部 7 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5579247

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档