如何在C#中创建函数数组?

时间:2015-02-09 04:41:41

标签: c# arrays winforms function for-loop

我有以下代码,它工作得很好,但我想减少for循环语句,就像创建一个数组函数一样,循环使用所有语句。这是我的代码:

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

现在我明白了。 谢谢大家的支持。

2 个答案:

答案 0 :(得分: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));

对你来说这不会有效吗?

答案 1 :(得分:1)

使用预定义的通用委托 Func&lt;&gt;和行动&lt;&gt;像这样:

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