Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hello, im making a textbased hungergames simulator in c#.
In my code i have a list with all players and their stats.
this list is made out of a class called Player with variables i nit like name,kills and an alive bool to check or the player is alive.

somtimes in my code i need to pick a random player that is alive. the problem wit this was that it taked too long to get a random player becouse at the late stages of the game njot manyy people where alive and the random generator always picked people who are dead and i cant use those.

so i decided to add a second list with players called AlivePlayers. it is the same as the normal players list onlt in this list i can remove the players from the lsit when they are dead. i cant do this in the players list becouse i need to keep the stats from the players.

so with that problem solved i started writing the code to actual remove the player from the list. and thats where things gor weird.

i first started with writing the code to remove the players fromt the alivePlayers list. i started my program and followed the values of the lists in th watch window. i then saw that the moment i runned the remove methode of the list that the items get removed from the alivePlayers list but ALSO from the players list. first i tought the problems was somwhere else in my code so i commented the remove part out and tried again. but know nothing gots removed so why does the remove methode removes from 2 lists ?

here is the part of the code that does it:
C#
if(action.Element("Action").Element("lethalto").Value != "none")
     {
         string rawString = action.Element("Action").Element("lethalto").Value;
         string[] playersToKill = rawString.Split('-');

         foreach(string player in playersToKill)
         {
             string playerIdString = player.Replace('p', ' ');
             int playerId = Int32.Parse(playerIdString);
             SurvivalGames.alivePlayers.Remove(affectedPlayers[playerId - 1]);

         }
     }

dont focus at rawString and playersToKill. my code is written so that it is customizable with xml. those variables are only for getting the indexes of the players that need to be removed. those 2 work fine i already tested them.

affectedPlayers is a list of players that are affected. in this case i want to remove the affected players form the alivePlayers list. the playerId is the index of the player from the affected players list that we need to remove. i substract 1 so that i do not get an out of bounds error.

so someone knows why the remove() methode deleted from both lists and not only from alivePlayers ?

ps: sorry for my bad english it is not my primary language. and feel free to ask questions if you do not understand something.

What I have tried:

searching on google but i did not found anything related to my problem.
Posted
Updated 4-Apr-19 3:32am

1 solution

Based on your description, you have two variables pointing to the same instance of List<T>. Any changes made to that instance will obviously be visible whichever variable you use to access it.
C#
List<int> x = new List<int>();
List<int> y = x; // Points to the same instance.

x.Add(42);

Console.WriteLine(x.Length); // Output: 1
Console.WriteLine(y.Length); // Output: 1

y.Remove(42);

Console.WriteLine(x.Length); // Output: 0
Console.WriteLine(y.Length); // Output: 0

What you need is a separate instance of List<T>.
NB: If an item needs to be in both lists, then you will need to add it to both lists.
C#
List<int> x = new List<int>();
List<int> y = new List<int>(); // Two separate instances.

x.Add(42);
Console.WriteLine(x.Length); // Output: 1
Console.WriteLine(y.Length); // Output: 0

y.Add(42);
Console.WriteLine(x.Length); // Output: 1
Console.WriteLine(y.Length); // Output: 1

y.Remove(42);
Console.WriteLine(x.Length); // Output: 1
Console.WriteLine(y.Length); // Output: 0
 
Share this answer
 
Comments
BillWoodruff 6-Apr-19 18:09pm    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900