简体   繁体   English

如何优化此代码?

[英]How to optimize this code?

Surely there has to be many ways to optimize the following code, where I basically have to make sure that a lot of textboxes aren't empty and then read their values: 当然,必须有很多方法来优化下面的代码,我必须确保很多文本框都不是空的,然后读取它们的值:

if (foo1.Text.Length != 0 & bar1.Text.Length != 0)
{
    output.Text += myStrings[i] + " / " + foo1.Text + " / " + bar1.Text;
}

if (foo2.Text.Length != 0 & bar2.Text.Length != 0)
{
    output.Text += myStrings[i] + " / " + foo2.Text + " / " + bar2.Text;
}

if (foo3.Text.Length != 0 & bar3.Text.Length != 0)
{
    output.Text += myStrings[i] + " / " + foo3.Text + " / " + bar3.Text;
}

if (foo4.Text.Length != 0 & bar4.Text.Length != 0)
{
    output.Text += myStrings[i] + " / " + foo4.Text + " / " + bar4.Text;
}

if (foo5.Text.Length != 0 & bar5.Text.Length != 0) 
{
    output.Text += myStrings[i] + " / " + foo5.Text + " / " + bar5.Text;
}

if (foo6.Text.Length != 0 & bar6.Text.Length != 0)
    output.Text += myStrings[i] + " / " + foo6.Text + " / " + bar6.Text;

if (foo7.Text.Length != 0 & bar7.Text.Length != 0)
{
    output.Text += myStrings[i] + " / " + foo7.Text + " / " + bar7.Text;
}

if (foo8.Text.Length != 0 & bar8.Text.Length != 0)
{
    output.Text += myStrings[i] + " / " + foo8.Text + " / " + bar8.Text;
}

if (foo9.Text.Length != 0 & bar9.Text.Length != 0)
{
    output.Text += myStrings[i] + " / " + foo9.Text + " / " + bar9.Text;
}

if (foo10.Text.Length != 0 & bar10.Text.Length != 0)
{
    output.Text += myStrings[i] + " / " + foo10.Text + " / " + bar10.Text;
}

I would put the repeated elements in arrays and then loop over them. 我会将重复的元素放在数组中,然后循环遍历它们。

TextBox[] foos = new TextBox[] { foo1, foo2, foo3, /* etc */ };
TextBox[] bars = new TextBox[] { bar1, bar2, bar3, /* etc */ };

for (int i = 0; i <= 10; i++)
    if (foos[i].Text.Length != 0 && bars[i].Text.Length != 0)
        output.Text += myStrings[i] + "/" + foos[i].Text + bars[i].Text;

Of course, if the elements are really named sequentially you can fill the arrays by looking up the controls from the form's Controls collection, with the name "foo" + number.ToString(). 当然,如果元素确实按顺序命名,则可以通过查找表单的Controls集合中的控件来填充数组,名称为“foo”+ number.ToString()。

I would just loop over the Controls collection on which these TextBoxes reside, then filter on TextBox only and do you checks and concat. 我将循环遍历这些TextBox所在的Controls集合,然后仅对TextBox进行过滤,并进行检查和连接。

I also strongly advice to use a StringBuilder instead of +=. 我也强烈建议使用StringBuilder而不是+ =。

There are a lot of ways to refactor this. 有很多方法可以重构这个。 The method you choose will depend on your particular scenario and needs. 您选择的方法取决于您的特定方案和需求。

  1. Make a function that takes a foo and a bar as a parameter and returns the string, then aggregate that string in a string builder 创建一个以foo和bar为参数并返回字符串的函数,然后在字符串生成器中聚合该字符串
  2. Put the foos and bars into collections and loop over those collections. 将foos和bars放入集合中并循环遍历这些集合。 In this scenario, arrays would be useful and provide a way to mutually index the arrays. 在这种情况下,数组将是有用的,并提供了一种相互索引数组的方法。
  3. Take item 2 one step further and create a new class FooBar that holds a foo and a bar together. 将项目2更进一步,创建一个新的类FooBar,它将foo和bar放在一起。 This way you can create a collection of FooBars and you don't have an implicit association between them anymore, now it's explicit and codified. 通过这种方式,您可以创建一个FooBars集合,并且它们之间不再存在隐式关联,现在它是明确的和编码的。
  4. Take item 3 one step further and recognize that you're aggregating a string. 进一步采取第3项,并认识到你正在聚合一个字符串。 If you're in a recent version of c#, take advantage of your Map/Reduce in LINQ (.Select().Aggregate()) to transform your FooBars into their corresponding strings, and then to aggregate the strings into output. 如果您使用的是最新版本的c#,请利用LINQ中的Map / Reduce(.Select(。。Aggregate())将您的FooBars转换为相应的字符串,然后将字符串聚合到输出中。

This is all just the stuff off the top of my head. 这一切都只是我头脑中的东西。 If you work at it more, I'm sure you can do even better. 如果你更多地工作,我相信你可以做得更好。 :) :)

(If this is homework, please add a homework tag.) (如果这是作业,请添加作业标签。)

EDIT: 编辑:

I can't help but wonder about the design of the UI in general based upon your comment in another post stating that it takes "many seconds" to concatenate the strings together. 我不禁想知道UI的设计一般基于你在另一篇帖子中的评论,说明将字符串连接在一起需要“很多秒”。 10 strings is a pretty trivial amount of time on its own, but this suggests that your outer loop (that's generating i ) is fairly long running. 10个字符串本身就是一个非常微不足道的时间,但这表明你的外部循环(即生成i )运行时间相当长。

If you're in a position where you have the freedom to make such decisions, are you certain that your UI is actually good for the task at hand? 如果您处于可以自由做出此类决策的位置,您确定您的UI实际上对于手头的任务有用吗? A large collection of pairs of text boxes is a difficult user interface in general. 一般来说,大量文本框对是一个困难的用户界面。 Perhaps a ListView would be more appropriate. 也许ListView更合适。 It would implicitly contain a collection, so you wouldn't have to deal with this "ten text boxes" silliness and will be an easier UI for your users to follow in general. 它会隐含地包​​含一个集合,因此您不必处理这个“十个文本框”的愚蠢,并且将是一个更容易的UI供您的用户一般遵循。

Write a function which accepts foo & bar types. 编写一个接受foo和bar类型的函数。 Pass all foo1 & bar1 to foo10 and bar10 to the function to get the values. 将所有foo1和bar1传递给foo10和bar10到函数以获取值。 You can also create array of foo and bar and you can loop and call the method to get the string. 你也可以创建foo和bar数组,你可以循环调用方法来获取字符串。

 foreach (Control ctrl in Page.Controls) {
      if (ctrl is TextBox) {
          if (ctrl.Text.Length != 0) {
              output.Text += myStrings[i] + "/" + ctrl.Text;
           }
      }
 }

Untested , but should work. 未经测试,但应该工作。 With this your textboxes could be named anything. 有了这个,您的文本框可以命名为任何东西。

Could you make foo1-foo10 into an array, foo[10], and the same for bar? 你能把foo1-foo10变成一个数组,foo [10],同样吧吧? That would allow you to express this as a simple loop. 这将允许您将其表达为一个简单的循环。

Would it be feasible to make a WebControl that has textboxes called foo and bar, and that has a function like: 制作一个名为foo和bar的文本框的WebControl是否可行,它具有如下函数:

if (foo.Text.Length != 0 & bar.Text.Length != 0)
    return myStrings[i] + " / " + foo.Text + " / " + bar.Text;
else
    return string.Empty;

Put ten of those on your page, then use: 将十个这些放在您的页面上,然后使用:

output.Text = myControl1.FooBar + myControl2.FooBar + myControl3.FooBar + ...

(Still a bit messy, but not quite so repetitive.) (仍然有点混乱,但不是那么重复。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM