mercredi 25 novembre 2015

C# If loop within a for loop within an if loop

I would like to reduce this code if I can:

class Alarm
{
    internal static void isGreaterThanOrBelowValue(int min, int max, int now, int i)
    {
        MainWindow mw = new MainWindow();

        if (now < min && now !=0)
        {
            if(i == 1)
            {
                mw.TxtBox1.Foreground = new SolidColorBrush(Colors.Red);
            }
            if (i == 2)
            {
                mw.TxtBox2.Foreground = new SolidColorBrush(Colors.Red);
            }
            if (i == 3)
            {
                mw.TxtBox3.Foreground = new SolidColorBrush(Colors.Red);
            }
        }
        if (now > max && now !=0)
        {
            if(i == 1)
            {
                mw.TxtBox1.Foreground = new SolidColorBrush(Colors.Red);
            }
            if (i == 2)
            {
                mw.TxtBox2.Foreground = new SolidColorBrush(Colors.Red);
            }
            if (i == 3)
            {
                mw.TxtBox3.Foreground = new SolidColorBrush(Colors.Red);
            }
        }

}

I want to do the following:

i can be between 1 and 33.

I can get the textbox name (TxtBox1) etc.

I would like to reduce the if statements so I don't have 32 if statements for each larger if statement.

Thank you! :)

Aucun commentaire:

Enregistrer un commentaire