1

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);
}
}
1
  • 2
    Why not just create a Dictionary<string, List<MonsterStatistics>> and add as many lists to that as you want? Commented Dec 3, 2017 at 12:51

1 Answer 1

2

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) });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.