简体   繁体   中英

c# creating multiple lists within for statement

Is there a way I can create multiple lists under a "for" statement, with an incrimental naming system (eg monster1, monster2, monster3). I've flagged it up under this is where I want an incrimental name within the code.

The number of lists needed to be generated is defined earlier on and I'm having no issues with that, but I'm getting "error CS0128: A local variable named `...' is already defined in this scope" if I attempt to assign the list name based on a variable with an inbuilt counter to incriment the list names (monsterList in this case).

Is there another way I can do this, so that no matter how many lists I need to be generated, it will create them and name them following this set pattern?

The below code is what I'm looking to have iterate a nuber of times (I'm new, so it's probably horribly inefficient, I'm just trying to get it doing what I want for now!):

for (int monstCounter = 2; monstCounter < totalTimes; monstCounter++) 
          {
Console.WriteLine();
string monsterLists = "Monster " + monstCounter;
string monsterList = "monsters"+monstCounter;
Console.WriteLine(monsterLists);
    p = 0;
    foreach (Monster aMonster in monsters)
    {
      if (monsters[p].MonsterName == monsterLists)
      y = p;
      p++;
    }
    y = monsters[y].MonsterPopu;

    //Create a list of individuals.
    List<MonsterStatistics> **This is where I want an incrimental name** = new List<MonsterStatistics>();
      totalTimes1 = y;
      counter1 = 1;
        for (int b=0; b < totalTimes1; b++) 
          {

    // Add monsters to the list.
      monsterList.Add(new MonsterStatistics() {oldAge = rndNum.Next(1,100), name = "Name"+counter1, coupled = false, genderGen = rndNum.Next(0,2)}); 
    counter1++;
}  

foreach (MonsterStatistics aMonster in monsterList){
          if(aMonster.genderGen == 0)
          aMonster.gender = "Male";
          else if (aMonster.genderGen == 1)
          aMonster.gender = "Female";
          else 
          aMonster.gender = "Genderless";
aMonster.age = rndNum.Next(0,aMonster.oldAge);
Console.WriteLine(aMonster.name + " Age: " + aMonster.age + "/" + aMonster.oldAge + " " + aMonster.gender);
}
}

You can't create variables from a string, its not how variables work in C#.

What you cand o is use a Dictionary and assign for each monsters list a unique key (string), something like:

Dictionary<string, List<MonsterStatistics>> monstersStatistics = new Dictionary<string, List<MonsterStatistics>>();

// Add a monster (here you can name monster with an incrementing variable)
monstersStatistics.Add("monster1", new List<MonsterStatistics>());
// Then fill the monster's statistics
monstersStatistics["monster1"].Add(new MonsterStatistics() { oldAge = rndNum.Next(1, 100), name = "Name" + counter1, coupled = false, genderGen = rndNum.Next(0, 2) });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM