首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
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

回答 7

Stack Overflow用户

发布于 2011-04-07 17:59:41

Switch语句删除所有if语句-

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

代码语言:javascript
复制
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是更改的值):

代码语言:javascript
复制
int result = bunkerLut[resipeDosings[i].Bunker];
modbus_master.SetValue("x1", Convert.ToInt32(resipeDosings[i].Massa) * result, 1);

其他选项是将值存储在配置文件或数据库中。

票数 7
EN

Stack Overflow用户

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

您可以执行以下操作:

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

https://stackoverflow.com/questions/5579247

复制
相关文章

相似问题

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