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

Time:10-23

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

CodePudding user response:

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)

CodePudding user response:

I don’t think that you really need a loop here. You can simply write like this.

attack1[0] = attacks[0];
attack1[1] = Convert.ToString(atdmg[0]);
attack1[2] = ateffect[0];
attack1[3] = Convert.ToString(ataccur[0]);
attack1[4] = Convert.ToString(atmul[0]);

And similarly for other items.

You can also wrap these lines in a method and call the method based by passing the respective attack object.

CodePudding user response:

As far as I know you cannot change a variable's name like

attacki

A suggestion could be creating a function to do the job inside the loop. This function could take the array, i, and the other necessary data.

for(int i=0; i<4;   i)
{
   if(i==0)
  

doSomething(attack1, i, ...) else if(i==1) ... }

and then

public void doSomething(array, no, ...)
{
   do your stuff here
}

EDIT: You don't really need a loop either anyway. You can just do:

doSomething(attack1, 0, ...)
doSomething(attack2, 1, ...)
doSomething(attack3, 2, ...)
doSomething(attack4, 3, ...)
  • Related