C# for loop code --change two values at once each "loop"

i have this code:

if (JoyData[4] == 1)
                {
                    Trigger.Checked = false;
                }
                else
                {
                    Trigger.Checked = true;
                }

                if (JoyData[5] == 1)
                {
                    Middle.Checked = false;
                }
                else
                {
                    Middle.Checked = true;
                }

                if (JoyData[6] == 1)
                {
                    Lower.Checked = false;
                }
                else
                {
                    Lower.Checked = true;
                }

                if (JoyData[7] == 1)
                {
                    Base.Checked = false;
                }
                else
                {
                    Base.Checked = true;
                }

it checks joydata array “places” 4->7 if they are true or false(it is backwards, 0 is enabled, 1 is open), and sets a “radioButton” checkbox accordingly on a C# form. joydata 4->7 array values are read from 4 buttons connected to a Arduino.

i figured it would make sense to put it in a for loop:

for (int v = 4; v < 8; ++x)
                {
                    if (JoyData[v] == 1)
                    {
                        Trigger.Checked = false;
                    }
                    else
                    {
                        Trigger.Checked = true;
                    }
                }

that was easy for checking the array values 4->7, but i need to check other buttons as i check the array values. array value 4 is trigger, array value 5 is middle…i need those to change along with “v” in the for loop. i thought of using a string array: string[] BtnName = new string[]{"Trigger","Middle","Lower","Base"}; but im not sure how to “sync” the btnName array with the 4->7 joydata array values in the for loop.

thanks :slight_smile:

A simple subtraction, or, for more flexibility, put the 4..7 range into an array too, then the indices line up.

Unless you plan to loop through all the controls in the form, comparing names, having an array of names isn't going to do you any good.

BtnName[n].Checked(); won't work, because string (the type of BtnName[n]) does not have a Checked() method.

Dang it! i always way overthink these issues...just simple subtraction...grrr

i see what you mean paulS, anything i can do to fix?

You could loop through all the controls on the form, to find the one with the name that matched the value in the nth position of the array, but, really, it seems like more effort than is required to just write 8 lines of code to set 4 controls.