簡體   English   中英

如何優化此代碼?

[英]How to optimize this code?

當然,必須有很多方法來優化下面的代碼,我必須確保很多文本框都不是空的,然后讀取它們的值:

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

我會將重復的元素放在數組中,然后循環遍歷它們。

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()。

我將循環遍歷這些TextBox所在的Controls集合,然后僅對TextBox進行過濾,並進行檢查和連接。

我也強烈建議使用StringBuilder而不是+ =。

有很多方法可以重構這個。 您選擇的方法取決於您的特定方案和需求。

  1. 創建一個以foo和bar為參數並返回字符串的函數,然后在字符串生成器中聚合該字符串
  2. 將foos和bars放入集合中並循環遍歷這些集合。 在這種情況下,數組將是有用的,並提供了一種相互索引數組的方法。
  3. 將項目2更進一步,創建一個新的類FooBar,它將foo和bar放在一起。 通過這種方式,您可以創建一個FooBars集合,並且它們之間不再存在隱式關聯,現在它是明確的和編碼的。
  4. 進一步采取第3項,並認識到你正在聚合一個字符串。 如果您使用的是最新版本的c#,請利用LINQ中的Map / Reduce(.Select(。。Aggregate())將您的FooBars轉換為相應的字符串,然后將字符串聚合到輸出中。

這一切都只是我頭腦中的東西。 如果你更多地工作,我相信你可以做得更好。 :)

(如果這是作業,請添加作業標簽。)

編輯:

我不禁想知道UI的設計一般基於你在另一篇帖子中的評論,說明將字符串連接在一起需要“很多秒”。 10個字符串本身就是一個非常微不足道的時間,但這表明你的外部循環(即生成i )運行時間相當長。

如果您處於可以自由做出此類決策的位置,您確定您的UI實際上對於手頭的任務有用嗎? 一般來說,大量文本框對是一個困難的用戶界面。 也許ListView更合適。 它會隱含地包​​含一個集合,因此您不必處理這個“十個文本框”的愚蠢,並且將是一個更容易的UI供您的用戶一般遵循。

編寫一個接受foo和bar類型的函數。 將所有foo1和bar1傳遞給foo10和bar10到函數以獲取值。 你也可以創建foo和bar數組,你可以循環調用方法來獲取字符串。

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

未經測試,但應該工作。 有了這個,您的文本框可以命名為任何東西。

你能把foo1-foo10變成一個數組,foo [10],同樣吧吧? 這將允許您將其表達為一個簡單的循環。

制作一個名為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 + ...

(仍然有點混亂,但不是那么重復。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM