I have the following:
public class Animal
public int currentPopulation
public string name
public Animal(int currentPopulation, string name){
this.currentPopulation = currentPopulation;
this.name = name;
}
In another class I have:
public class MainClass
<List><Animal>animalList
...
lion = newAnimal(100, "Lion");
cat = newAnimal(20, "Cat");
dog = newAnimal(40, "Dog")
animalList.add(lion);
animalList.add(cat);
animalList.add(dog);
Every so often I have to fetch new data from a server and update the animal property, currentPopulation in the MainClass. Currently I'm doing this by the following:
public void UpdatePopulations(int lionPopulation, int catPopulation, int dogPopulation)
foreach(var item in animalList.where(n=>n.name=="Lion")){
item.currentPopulation = lionPopulation;
}
... and the same for the cat and dog.
I feel like my solution is bulky and I'm wondering if there is a cleaner way to update the objects from the list.