Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change which array I want to modify in a loop without having to type each of them

So I want to change many arrays in a similar way but I’m wondering if instead of doing something like this

for (int i = 0; i < 4; i++)
            {
                if(i==0)
                {
                    attack1[0] = attacks[i];
                    attack1[1] = Convert.ToString(atdmg[i]);
                    attack1[2] = ateffect[i];
                    attack1[3] = Convert.ToString(ataccur[i]);
                    attack1[4] = Convert.ToString(atmul[i]);
                }
                if (i == 1)
                {
                    attack2[0] = attacks[i];
                    attack2[1] = Convert.ToString(atdmg[i]);
                    attack2[2] = ateffect[i];
                    attack2[3] = Convert.ToString(ataccur[i]);
                    attack2[4] = Convert.ToString(atmul[i]);
                }
                if (i == 2)
                {
                    attack3[0] = attacks[i];
                    attack3[1] = Convert.ToString(atdmg[i]);
                    attack3[2] = ateffect[i];
                    attack3[3] = Convert.ToString(ataccur[i]);
                    attack3[4] = Convert.ToString(atmul[i]);
                }
                if (i == 3)
                {
                    attack4[0] = attacks[i];
                    attack4[1] = Convert.ToString(atdmg[i]);
                    attack4[2] = ateffect[i];
                    attack4[3] = Convert.ToString(ataccur[i]);
                    attack4[4] = Convert.ToString(atmul[i]);
                }

            }

I could do something like this

for (int i = 0; i < 4; i++)
            {
                    attacki[0] = attacks[i];
                    attacki[1] = Convert.ToString(atdmg[i]);
                    attacki[2] = ateffect[i];
                    attacki[3] = Convert.ToString(ataccur[i]);
                    attacki[4] = Convert.ToString(atmul[i]);
            }

so that I don’t have to type each one. The point of the code is to put information from other arrays into other arrays to simplify printing information in the long run

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can create a function updateAttacks that does this

public void updateAttacks(attack) 
    {
         attack[0] = attacks[i];
         attack[1] = Convert.ToString(atdmg[i]);
         attack[2] = ateffect[i];
         attack[3] = Convert.ToString(ataccur[i]);
         attack[4] = Convert.ToString(atmul[i]);
    }

and then in your main code call it like this:

updateAttacks(attack1)
updateAttacks(attack2)
updateAttacks(attack3)
updateAttacks(attack4)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading