0

このコードを単純化する方法はありますか? 同じタイプの複数のオブジェクトに同じプロパティを設定したいだけです。これらのオブジェクトは、Windows 8 アプリの TextBoxes です。Array と foreach で何かできると思いますが、よくわかりません ありがとう

private void makeVisible(int x)
        {
            if (x == 1)
            {
                field2.Visibility = Visibility.Visible;
                field3.Visibility = Visibility.Visible;
                field4.Visibility = Visibility.Visible;
                field6.Visibility = Visibility.Visible;
                field8.Visibility = Visibility.Visible;
                field9.Visibility = Visibility.Visible;
                field11.Visibility = Visibility.Visible;
            }

            if (x == 0)
            {
                field2.Visibility = Visibility.Collapsed;
                field3.Visibility = Visibility.Collapsed;
                field4.Visibility = Visibility.Collapsed;
                field6.Visibility = Visibility.Collapsed;
                field8.Visibility = Visibility.Collapsed;
                field9.Visibility = Visibility.Collapsed;
                field11.Visibility = Visibility.Collapsed;
                errorReporter.Visibility = Visibility.Collapsed;
            }
        }
4

4 に答える 4

0
private void makeVisible(int x)
{
    var boxes = new TextBox[] { field2, field3... };
    foreach (TextBox box in boxes)
    {
        box.Visibility = (x == 1) ? Visibility.Visible : Visibility.Collapsed;
    }

    if (x == 0)
    {
        errorReporter.Visibility = Visibility.Collapsed;
    }
}
于 2013-06-25T15:00:32.450 に答える