-2

There is an illogical change happening in my array called "animals". Whenever I change one element in the array, all the other elements change along with it. I have no idea why this is happening.

When I run the program below, the console writes

Sugar, Dog
Sugar, Dog

That should not be happening. I should be getting

Fluffy, Cat
Sugar, Dog

Please look at the relevant code below:

//Program.cs
namespace AnimalProgram
{
    class Program
    {
        static void Main(string[] args)
        {           
            AnimalInfo vitalStats = new AnimalInfo("vital statistics", new string[] { "name", "sex", "species", "breed", "age" }); //name, species, breed, sex, age.
            AnimalInfo veterinarian = new AnimalInfo("veterinarian", new string[] { "Vet Name", "Name of Vet's Practice" });

        List<AnimalInfo> animalStats = new List<AnimalInfo> {vitalStats, veterinarian };

        Animal cat1 = new Animal();
        Animal dog1 = new Animal();

        Animal[] animals = new Animal[2] { cat1, dog1};

        for(int i = 0; i < animals.Count(); i++) animals[i].initializeAnimalInfo(animalStats);

        AnimalInfo cat1vitals= new AnimalInfo("vital statistics", new string[] { "Fluffy", "F", "Cat", "American Shorthair", "5" });
        AnimalInfo dog1vitals = new AnimalInfo("vital statistics", new string[] { "Sugar", "F", "Dog", "Great Dane", "7" });
        AnimalInfo cat1vet = new AnimalInfo("veterinarian", new string[] { "Joe Schmoe", "Joe's Veterinary" });
        AnimalInfo dog1vet = new AnimalInfo("veterinarian", new string[] { "Jim Blow", "Jim's Garage" });

        cat1.UpdateAnimalInfo(new List<AnimalInfo>() { cat1vitals, cat1vet });
        dog1.UpdateAnimalInfo(new List<AnimalInfo>() { dog1vitals, dog1vet });

        Console.WriteLine(cat1.animalProperties[0].info[0] + ", " + cat1.animalProperties[0].info[2]);
        Console.WriteLine(dog1.animalProperties[0].info[0] + ", " + dog1.animalProperties[0].info[2]);

        Console.ReadLine();
    }
}

}

//Animal.cs
using System.Collections.Generic;
using System.Linq;

namespace AnimalProgram
{

class Animal
{

    public List<AnimalInfo> animalProperties;

    public Animal() {  }

    public void initializeAnimalInfo(List<AnimalInfo> aInfo)
    {
        animalProperties = aInfo;
    }

    public void UpdateAnimalInfo(List<AnimalInfo> targetInfo)
    {
        for (int i = 0; i < targetInfo.Count(); i++)
            animalProperties[i].info = targetInfo[i].info;
    }

}

}

//AnimalInfo.cs
namespace AnimalProgram
{
    public class AnimalInfo
    {
        public string infoName;
        public string [] info;

    public AnimalInfo(string iName, string [] information)
    {
        infoName = iName;
        info = information;
    }
}

}

Dustin
  • 1
  • 2
    `initializeAnimalInfo` sets the `animalProperties` for each `Animal` to a reference to the same list. You’re abusing lists wayyy too much here, though. Use… dictionaries or something? – Ry- Dec 22 '17 at 05:16

1 Answers1

1

You initialize animalStats with the same objects. So, dog and cat keep share one set of properties.

You should create own properties for every object:

for(int i = 0; i < animals.Count(); i++) {
    AnimalInfo vitalStats = new AnimalInfo("vital statistics", new string[] { "name", "sex", "species", "breed", "age" }); //name, species, breed, sex, age.
    AnimalInfo veterinarian = new AnimalInfo("veterinarian", new string[] { "Vet Name", "Name of Vet's Practice" });

    List<AnimalInfo> animalStats = new List<AnimalInfo> {vitalStats, veterinarian };
    animals[i].initializeAnimalInfo(animalStats);
}
Backs
  • 24,430
  • 5
  • 58
  • 85