   [.net] C# problem
  I have an WeaponContainer class that im using to read weapon data from an xml file, a weapon can contain serval "pipes", so i have an ArrayList that i clear on each <Weapon> tag that holds the pipe locations (vector). When im on the end of an </weapon> i add an instance of a Weapon Class to an ArrayList in WeaponContainer that holds all weapons.. (weaponList.Add(new Weapon(pipes, "bullet", bSpeed));) pipes are the name of my ArrayList thats is filled up with... pipes. The class Weapon got an ArrayList that copy the pipes ArrayList into an new one thats in the class.My problem is that all my weapons... even the first in my list of weapons has the same pipe list. Do you know what the problem is?. Here is my code for loading the weapons from the xml:   public static void LoadWeapons(string fileName)   {  ArrayList pipes = new ArrayList();  string bTexture = "";  string weaponName = "";  float bSpeed=0;  using (XmlReader xmlReader = XmlReader.Create(fileName))  { while (xmlReader.Read()) {if (xmlReader.NodeType==XmlNodeType.Element && xmlReader.Name=="Weapon"){    pipes.Clear();    Console.WriteLine("cleared pipe list"); weaponName = xmlReader.GetAttribute("Name"); while (xmlReader.Read() && !(xmlReader.NodeType == XmlNodeType.EndElement && xmlReader.Name == "Weapon")) {if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name == "bulletMaxSpeed"){    bSpeed = int.Parse(xmlReader.ReadElementContentAsString());}if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name == "Pipe"){    int xpos = 0, ypos = 0;    while (xmlReader.Read() && !(xmlReader.NodeType == XmlNodeType.EndElement && xmlReader.Name == "Pipe"))    {   if (xmlReader.Name == "pipeLocationX") xpos = int.Parse(xmlReader.ReadElementContentAsString());   if (xmlReader.Name == "pipeLocationY") ypos = int.Parse(xmlReader.ReadElementContentAsString());    }    pipes.Add(new Vector2(xpos, ypos));} }}if (xmlReader.NodeType == XmlNodeType.EndElement && xmlReader.Name == "Weapon"){    weaponList.Add(new Weapon(pipes, "bullet", bSpeed));} } xmlReader.Close();  }   }
  It's hard to tell form your description how this is implemented in the class. Anyway the function you posted doesn't seem to do anything. The ArrayList is a local variable in a static function, since I can't see where you're assigning it I'm going to guess it's erased when it goes out of scope(the function returns).   ;  Found the problem... it was in the constructor of the Weapon class.this.weaponPipes = pipes;That made weaponPipes a pointer to pipes.this.weaponPipes = new ArrayList(pipes);Did the trick and made copy instead.;  You may want to think about using Generics, i.e. a typed container instead of the untyped ArrayList, something like System.Collections.Generics.List<Vector2>. Generics are similar to C++ templates and give you type safety.Skizz   ;  You may also want to read up on the difference between reference and value types in C# and what happens in the conversion.   