如何简化选择(如果)
我有这样的代码可以使其更好(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Switch 语句删除了所有
if
语句 -但是,有两件事是显而易见的:
构建 LUT 的最简单方法是使用
Dictionary
。然后你可以查找类似这样的内容(假设 10 是更改的值):
其他选项是将值存储在配置文件或数据库中。
Switch statement removes all the
if
statements -However, two things are obvious:
The easiest way of building a LUT is using a
Dictionary<>
.Then you can look up, something like this (assuming the 10 is the value that changes):
Other options are storing the values in a configuration file or database.
您可以执行以下操作:
You can do the following:
嗯,只要你对待他们的方式没有区别:
Uhm, as long as there is no difference in how you treat them:
由于对不同值执行的操作似乎没有区别,因此您只需验证它是否是要检查的值之一,并在这种情况下执行操作:
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:
我可能会忽略一些东西,但每个掩体情况都没有区别。
这会起到完全相同的作用。
I could be overlooking something, but there is no difference for each bunker case.
This would do exactly the same.
还有一件事:
如果您按照您的方式使用
if
,即检查一次只能有一个为 true,请使用else if
,这样它就不会计算以下表达式:已知是错误的:但只有在没有更好的选择(例如
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, useelse if
, so it will not evaluate expressions that are known to be false:But do so only, if there is no better alternative like
switch
or something else...我有一本字符串/函数字典,像这样;
我正在做出几个假设,并且不知道没有更多示例代码/示例匹配等的实际要求是什么。
编辑:在阅读其他答案的一些评论后,您可以很容易地将其修改为正则表达式的字典/功能。并从工艺方法来看;如果“Bunker”属性与正则表达式匹配,则调用该函数。
I'd have a dictionary of string/func, something like this;
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.