2015-02-09 37 views
0

我有下面的代碼,它工作的很好,但我想減少for循環語句就像創建一個數組函數循環用於所有語句的唯一一個。這裏是我有的代碼:如何在C#中創建一個函數數組?

string Combine1 = "", result1 = ""; 
TextBox[] StoreGame1 = new TextBox[5] { 
    txtGameOne1, txtGameOne2, txtGameOne3, txtGameOne4, txtGameOne5 
}; 
string Combine2 = "", result2 = ""; 
TextBox[] StoreGame2 = new TextBox[5] { 
    txtGameTwo1, txtGameTwo2, txtGameTwo3, txtGameTwo4, txtGameTwo5 
}; 
string Combine3 = "", result3 = ""; 
TextBox[] StoreGame3 = new TextBox[5] { 
    txtGameThree1, txtGameThree2, txtGameThree3, txtGameThree4, txtGameThree5 
}; 
if (
StoreGame1[0].Text != "" && StoreGame1[1].Text != "" && StoreGame1[2].Text != "" && StoreGame1[3].Text != "" && StoreGame1[4].Text != "" && StoreGame2[0].Text != "" && StoreGame2[1].Text != "" && StoreGame2[2].Text != "" && StoreGame2[3].Text != "" && StoreGame2[4].Text != "" && StoreGame3[0].Text != "" && StoreGame3[1].Text != "" && StoreGame3[2].Text != "" && StoreGame3[3].Text != "" && StoreGame3[4].Text != "") { 
    // For Game 1 
    for (int i = 0; i < StoreGame1.Length; i++) { 
     Combine1 += StoreGame1[i].Text + "-"; 
    } 
    result1 = Combine1.Substring(0, Combine1.Length - 1); 

    // For Game 2 

    for (int i = 0; i < StoreGame2.Length; i++) { 
     Combine2 += StoreGame2[i].Text + "-"; 
    } 
    result2 = Combine2.Substring(0, Combine2.Length - 1); 

    // For Game 3 

    for (int i = 0; i < StoreGame3.Length; i++) { 
     Combine3 += StoreGame3[i].Text + "-"; 
    } 
    result3 = Combine3.Substring(0, Combine3.Length - 1); 

    if (txtBuyNumber.Text != "") { 
     long mobilenumber = long.Parse(txtBuyNumber.Text); 
     string submit = client.SubmitBuying("", 1, 1, result1 + ";" + result2 + ";" + result3, countgame, 1, "PC", mobilenumber, "Office"); 
     if (submit == "06") { 
      MessageBox.Show("Bet Success!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); 
     } else { 
      MessageBox.Show("System error please try again", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); 
     } 
    } else { 
     MessageBox.Show("Please enter select any customer phone number first!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); 
    } 
} else { 
    MessageBox.Show("Any game still empty please enter animal number before buy", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); 
    focusedTextbox.Focus(); 
} 

它的工作原理,但我怎麼能實現這個使用任何函數來減少for循環。

感謝,

編輯: 我試圖創建一個功能

private string DelimeterGameTypeOne(TextBox[] ArrValue) 
    { 
     string com = "", res = ""; 
     for(int i = 0; i < ArrValue.Length; i++) 
     { 
      com += ArrValue[i].Text + "-"; 
     } 
     return res = com.Substring(0, com.Length - 1); 
    } 

,並要求它像

// For Game 1 
result1 = DelimeterGameTypeOne(StoreGame1); 
// For Game 2 
result2 = DelimeterGameTypeOne(StoreGame2); 
// For Game 3 
result3 = DelimeterGameTypeOne(StoreGame3); 

現在我得到它的工作原理。 謝謝大家支持。

+0

仔細格式化您的代碼不會傷害,並幫助您很快得到答覆。 – David 2015-02-09 04:43:56

+0

是我現在幾乎無法讀取代碼\ 1 – chouaib 2015-02-09 04:45:33

+0

創建函數並接受'StoreGame'作爲輸入並執行您的邏輯,返回結果字符串。你可以調用這個函數,而不是每次都寫循環 – Thangadurai 2015-02-09 04:46:17

回答

1

使用預先定義的通用代表 Func鍵<>和行動<>是這樣的:

using System.Collections.Generic; 

Func<int,bool> [] funcArray = new Func<int,bool> []() { 
    Func1 
    ,Func2 
    ,(i) => (i >= 36) 
}; 

bool Func1 (int i) { return i <= 16; } 
bool Func2 (int I) { return I >= 25; } 
3

在我看來,你的for循環可以這樣反而表示:

// For Game 1 
result1 = string.Join("-", StoreGame1.Select(item => item.Text)); 
// For Game 2 
result2 = string.Join("-", StoreGame2.Select(item => item.Text)); 
// For Game 3 
result3 = string.Join("-", StoreGame3.Select(item => item.Text)); 

對你而言,這不就行嗎?

相關問題