如何简化选择(如果)

发布于 2024-10-30 19:18:58 字数 2321 浏览 2 评论 0原文

我有这样的代码可以使其更好(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);
            }
        }

I have such a code as to make it better (modbus_master.SetValue("x1", Convert.ToInt32(resipeDosings[i].Massa) * 10, 1); - sending data to the controller)

 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);
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

人│生佛魔见 2024-11-06 19:18:58

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
}

但是,有两件事是显而易见的:

  • 无论如何,您都在执行相同的代码
  • 您应该将变量(每个 Bunker 可能不同的东西)存储在一个外观中 -建立表或数据库表,因此您不需要每次获得新的 Bunker 或想要更改值时都修改程序

构建 LUT 的最简单方法是使用 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);

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

Switch statement removes all the if statements -

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
}

However, two things are obvious:

  • You're executing the same code regardless
  • You should probably store the variables (something that might be different for each Bunker) in a look-up table or database table, so you don't need to modify the program each time you get a new Bunker or want to change a value

The easiest way of building a LUT is using a 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

Then you can look up, something like this (assuming the 10 is the value that changes):

int result = bunkerLut[resipeDosings[i].Bunker];
modbus_master.SetValue("x1", Convert.ToInt32(resipeDosings[i].Massa) * result, 1);

Other options are storing the values in a configuration file or database.

海之角 2024-11-06 19:18:58

您可以执行以下操作:

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;
    }
}

You can do the following:

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;
    }
}
落墨 2024-11-06 19:18:58

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

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);      
    }
}

Uhm, as long as there is no difference in how you treat them:

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);      
    }
}
回忆凄美了谁 2024-11-06 19:18:58

由于对不同值执行的操作似乎没有区别,因此您只需验证它是否是要检查的值之一,并在这种情况下执行操作:

var valuesToCheck = new[] { 
    "Bunker 1",
    "Bunker 2",
    "Bunker 3",
    "Bunker 4",
    "Bunker 5",
    "Bunker 6",
    "Bunker 7",
    "Bunker 8",
    "Bunker 9"};

for (int i = 0; i < resipeDosings.Count; i++)
{                
    if (valuesToCheck.Contains(resipeDosings[i].Bunker)
    {
        modbus_master.SetValue("x1", Convert.ToInt32(resipeDosings[i].Massa) * 10, 1);      
    }    
}

Since there seems to be no difference in what is done for the different values, you can just verify that it is one of the values to check for, and perform the action if that is the case:

var valuesToCheck = new[] { 
    "Bunker 1",
    "Bunker 2",
    "Bunker 3",
    "Bunker 4",
    "Bunker 5",
    "Bunker 6",
    "Bunker 7",
    "Bunker 8",
    "Bunker 9"};

for (int i = 0; i < resipeDosings.Count; i++)
{                
    if (valuesToCheck.Contains(resipeDosings[i].Bunker)
    {
        modbus_master.SetValue("x1", Convert.ToInt32(resipeDosings[i].Massa) * 10, 1);      
    }    
}
月下伊人醉 2024-11-06 19:18:58

我可能会忽略一些东西,但每个掩体情况都没有区别。

for (int i = 0; i < resipeDosings.Count; i++)
    modbus_master.SetValue("x1", Convert.ToInt32(resipeDosings[i].Massa) * 10, 1);

这会起到完全相​​同的作用。

I could be overlooking something, but there is no difference for each bunker case.

for (int i = 0; i < resipeDosings.Count; i++)
    modbus_master.SetValue("x1", Convert.ToInt32(resipeDosings[i].Massa) * 10, 1);

This would do exactly the same.

骄兵必败 2024-11-06 19:18:58

还有一件事:
如果您按照您的方式使用 if,即检查一次只能有一个为 true,请使用 else if,这样它就不会计算以下表达式:已知是错误的:

if(cond1)
{
   // do stuff
}
else if(cond2)
{
   // do stuff
}
else if(cond3)
{
   // do stuff
}

但只有在没有更好的选择(例如 switch 或其他东西)时才这样做......

One more thing:
If you use if in the way you do, i.e. with checks of which only one can be true at a time, use else if, so it will not evaluate expressions that are known to be false:

if(cond1)
{
   // do stuff
}
else if(cond2)
{
   // do stuff
}
else if(cond3)
{
   // do stuff
}

But do so only, if there is no better alternative like switch or something else...

不羁少年 2024-11-06 19:18:58

我有一本字符串/函数字典,像这样;

class EmirateRefactored
{
    public List<RecipeDosings> resipeDosings = new List<RecipeDosings>();
    private Dictionary<string, Func<RecipeDosings, int>> m_objActionMapping;

    public EmirateRefactored()
    {
        m_objActionMapping = new Dictionary<string, Func<RecipeDosings, int>>();
        m_objActionMapping.Add("Bunker 1", bunker1);
        m_objActionMapping.Add("Bunker 2", bunker2);
        //etc
    }

    public void Process(ModBusMaster modbus_master)
    {
        foreach (RecipeDosings rd in resipeDosings)
        {
            if (m_objActionMapping.ContainsKey(rd.Bunker))
            {
                int result = m_objActionMapping[rd.Bunker].Invoke(rd);
                modbus_master.SetValue(result);
            }
            else
                throw new ApplicationException("Couldn't parse bunker!");
        }
    }

    /// <summary>
    /// I have no idea what this is as it's not included in the sample code!
    /// </summary>
    public class ModBusMaster
    {
        public void SetValue(int i_intInput) { }
    }

    /// <summary>
    /// Some code relevant to "bunker 1"
    /// </summary>
    private int bunker1(RecipeDosings i_objRecipe)
    {
        return Convert.ToInt32(i_objRecipe.Massa) * 10;
    }
    /// <summary>
    /// Some code relevant to "bunker 2"
    /// </summary>
    private int bunker2(RecipeDosings i_objRecipe)
    {
        //bunker2 code
        return Convert.ToInt32(i_objRecipe.Massa) * 10;
    }
}

我正在做出几个假设,并且不知道没有更多示例代码/示例匹配等的实际要求是什么。

编辑:在阅读其他答案的一些评论后,您可以很容易地将其修改为正则表达式的字典/功能。并从工艺方法来看;如果“Bunker”属性与正则表达式匹配,则调用该函数。

I'd have a dictionary of string/func, something like this;

class EmirateRefactored
{
    public List<RecipeDosings> resipeDosings = new List<RecipeDosings>();
    private Dictionary<string, Func<RecipeDosings, int>> m_objActionMapping;

    public EmirateRefactored()
    {
        m_objActionMapping = new Dictionary<string, Func<RecipeDosings, int>>();
        m_objActionMapping.Add("Bunker 1", bunker1);
        m_objActionMapping.Add("Bunker 2", bunker2);
        //etc
    }

    public void Process(ModBusMaster modbus_master)
    {
        foreach (RecipeDosings rd in resipeDosings)
        {
            if (m_objActionMapping.ContainsKey(rd.Bunker))
            {
                int result = m_objActionMapping[rd.Bunker].Invoke(rd);
                modbus_master.SetValue(result);
            }
            else
                throw new ApplicationException("Couldn't parse bunker!");
        }
    }

    /// <summary>
    /// I have no idea what this is as it's not included in the sample code!
    /// </summary>
    public class ModBusMaster
    {
        public void SetValue(int i_intInput) { }
    }

    /// <summary>
    /// Some code relevant to "bunker 1"
    /// </summary>
    private int bunker1(RecipeDosings i_objRecipe)
    {
        return Convert.ToInt32(i_objRecipe.Massa) * 10;
    }
    /// <summary>
    /// Some code relevant to "bunker 2"
    /// </summary>
    private int bunker2(RecipeDosings i_objRecipe)
    {
        //bunker2 code
        return Convert.ToInt32(i_objRecipe.Massa) * 10;
    }
}

I'm making several assumptions, and have no idea what the actual requirements are without more sample-code / example matches, etc, etc.

Edit: After reading some comments on other answers, you could quite easily modify this to a dictionary of Regex/func. And from the process method; if the "Bunker" property matches the regex Invoke the func.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文