如何优化此代码?

时间:2009-02-12 15:49:18

标签: c# optimization

当然,必须有很多方法来优化以下代码,我必须确保许多文本框不为空,然后读取它们的值:

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

7 个答案:

答案 0 :(得分:9)

我会将重复的元素放在数组中,然后循环遍历它们。

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;

当然,如果元素是按顺序命名的,你可以通过查找表单的Controls集合中的控件来填充数组,名称为“foo”+ number.ToString()。

答案 1 :(得分:5)

我只是循环遍历这些TextBox所在的Controls集合,然后仅对TextBox进行过滤,并进行检查和连接。

我也强烈建议使用StringBuilder而不是+ =。

答案 2 :(得分:2)

有很多方法可以重构这个。您选择的方法取决于您的特定方案和需求。

  1. 创建一个以foo和bar为参数并返回字符串,然后在字符串构建器中聚合该字符串的函数
  2. 将foos和bar放入集合中并循环遍历这些集合。在这种情况下,数组很有用,并提供了一种相互索引数组的方法。
  3. 进一步采取第2项,并创建一个新的类FooBar,它将foo和bar放在一起。通过这种方式,您可以创建一个FooBars集合,并且它们之间不再存在隐式关联,现在它已经明确并且编纂成了。
  4. 进一步采取第3项并确认您正在聚合字符串。如果您使用的是最新版本的c#,请利用LINQ中的Map / Reduce(.Select(。。Aggregate())将您的FooBars转换为相应的字符串,然后将字符串聚合为输出。
  5. 这就是我头脑中的一切。如果你更多地工作,我相信你可以做得更好。 :)

    (如果这是作业,请添加家庭作业标签。)

    编辑:

    我不禁想知道UI的设计一般是基于你在另一篇文章中的评论,说明将字符串连接起来需要“很多秒”。 10个字符串本身就是一个非常微不足道的时间,但这表明你的外部循环(生成i)的运行时间相当长。

    如果您处于可以自由做出此类决定的位置,您确定您的UI实际上对手头的任务有益吗?一般来说,大量文本框对是一个困难的用户界面。也许ListView更合适。它隐含地包含一个集合,因此您不必处理这个“十个文本框”的愚蠢,并且将是一个更容易的UI,供您的用户一般遵循。

答案 3 :(得分:1)

写一个接受foo&amp;的函数酒吧类型。通过所有foo1&amp; bar1到foo10和bar10到函数来获取值。你也可以创建foo和bar数组,你可以循环调用方法来获取字符串。

答案 4 :(得分:1)

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

未经测试,但应该有效。有了这个,你的文本框就可以命名了。

答案 5 :(得分:0)

你能把foo1-foo10变成一个数组,foo [10],同样吧吧?这将允许您将其表达为一个简单的循环。

答案 6 :(得分:0)

创建一个名为foo和bar的文本框的WebControl是否可行,并且具有如下函数:

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

将十个这些放在您的页面上,然后使用:

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

(仍然有点混乱,但不是那么重复。)

相关问题