locked
Problem with List<myType> RRS feed

  • Question

  • User206383436 posted

    I am testing the List<> class with a type of my own as shown below, adding 5 elements to it. My problem is that, at the end, I get the last item in all the 5 positions of the list. I followed the code with the debugger and I see that, every time an item is inserted, the old ones are replaced by the new one. I will very much appreciate your comments.

    Respectfully,
    Jorge Maldonado 

    protected void ASPxButton1_Click(object sender, EventArgs e)

    {
    List<Animales> lstAnimales = new List<Animales>();
    Animales objAnimales = new Animales();

    objAnimales.Clave = 1;
    objAnimales.Nombre = "Perro";
    lstAnimales.Add(objAnimales);

    objAnimales.Clave = 2;
    objAnimales.Nombre = "Gato";
    lstAnimales.Add(objAnimales);

    objAnimales.Clave = 3;
    objAnimales.Nombre = "Ratón";
    lstAnimales.Add(objAnimales);

    objAnimales.Clave = 4;
    objAnimales.Nombre = "Vaca";
    lstAnimales.Add(objAnimales);

    objAnimales.Clave = 5;
    objAnimales.Nombre = "Jirafa";
    lstAnimales.Add(objAnimales);
    }

    Wednesday, September 11, 2013 1:01 PM

Answers

  • User281315223 posted

    Depending on what your constructor looks like, you have a few options that you can do to handle this (however the current method is obviously going to cause problems since you are still re-referencing the same object).

    Option 1 : Create a new Object each time

    //Create your List
    List<Animales> lstAnimales = new List<Animales>();
    
    //Create Animal 1
    Animales a1 = new Animales();
    a1.Clave = 1;
    a1.Nombre = "Perro";
    lstAnimales.Add(a1);
    
    //Create Animal 2
    Animales a2 = new Animales();
    a1.Clave = 2;
    a1.Nombre = "Gato";
    lstAnimales.Add(a2);
    
    //Create Animal 3
    Animales a3 = new Animales();
    a1.Clave = 3;
    a1.Nombre = "Ratón";
    lstAnimales.Add(a3);

    //Additional Items below 

    Option 2 : Create a Constructor that will accepts your parameters and allow you to pass them in

    Using the following updated Constructor : 

    public class Animales
    {
         public int Clave { get; set;}
         public string Nombre { get; set; }
    
         //Empty Constructor
         public Animales()
         {
    
         }
    
         //Constructor that accepts your "Clave" and "Nombre" parameters
         public Animales(int clave, string nombre)
         {
             Clave = clave;
             Nombre = nombre;
         }
    }
    

    and then you could add your values individually like so : 

    //Create your List
    List<Animales> lstAnimales = new List<Animales>();
    
    //Create Animal 1
    lstAnimales.Add(new Animales(1,"Perro"));
    
    //Create Animal 2
    lstAnimales.Add(new Animales(2,"Gato"));
    
    //Create Animal 3
    lstAnimales.Add(new Animales(3,"Ratón"));
    
    //Additional Items below 

    Option 3 : Instantiate the List using your Items (similar to above)

    Another option rather than adding all of the items individually would be to simply create the List and automatically include all of these items : 

    //Create your List with your items already prepopulated (separated with commas)
    List<Animales> lstAnimales = new List<Animales>(){
                                          new Animales(1,"Perro"),
                                          new Animales(2,"Gato"),
                                          new Animales(3,"Ratón")
                                      };
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 11, 2013 1:41 PM

All replies

  • User-1360095595 posted

    You need to create a new object every time you want to add a new one to the list. So you need to do: objAnimales = new Animales(); after you add it to the list. 

    Wednesday, September 11, 2013 1:17 PM
  • User-183374066 posted

    Try Like following smarter way

    public class Animales
    {
        public int Clave { get; set; }
        public string Nombre { get; set; }
    }
    
    protected void ASPxButton1_Click(object sender, EventArgs e)
    {
        var lstAnimales = new List<Animales>
                                {
                                    new Animales {Clave = 1, Nombre = "Perro"},
                                    new Animales {Clave = 2, Nombre = "Gato"},
                                    new Animales {Clave = 3, Nombre = "Ratón"},
                                    new Animales {Clave = 4, Nombre = "Vaca"},
                                    new Animales {Clave = 5, Nombre = "Jirafa"}
                                };
    }

    OR

    You are missing initilizing of new objects. Change your code to

    protected void ASPxButton1_Click(object sender, EventArgs e)
    {
        List<Animales> lstAnimales = new List<Animales>();
        Animales objAnimales = new Animales();
    
        objAnimales.Clave = 1;
        objAnimales.Nombre = "Perro";
        lstAnimales.Add(objAnimales);
    
        Animales objAnimales2 = new Animales();
        objAnimales2.Clave = 2;
        objAnimales2.Nombre = "Gato";
        lstAnimales.Add(objAnimales2);
    
        Animales objAnimales3 = new Animales();
        objAnimales3.Clave = 3;
        objAnimales3.Nombre = "Ratón";
        lstAnimales.Add(objAnimales3);
    
        Animales objAnimales4 = new Animales();
        objAnimales4.Clave = 4;
        objAnimales4.Nombre = "Vaca";
        lstAnimales.Add(objAnimales4);
    
        Animales objAnimales5 = new Animales();
        objAnimales5.Clave = 5;
        objAnimales5.Nombre = "Jirafa";
        lstAnimales.Add(objAnimales5);
    }

    Regards.

    Wednesday, September 11, 2013 1:31 PM
  • User281315223 posted

    Depending on what your constructor looks like, you have a few options that you can do to handle this (however the current method is obviously going to cause problems since you are still re-referencing the same object).

    Option 1 : Create a new Object each time

    //Create your List
    List<Animales> lstAnimales = new List<Animales>();
    
    //Create Animal 1
    Animales a1 = new Animales();
    a1.Clave = 1;
    a1.Nombre = "Perro";
    lstAnimales.Add(a1);
    
    //Create Animal 2
    Animales a2 = new Animales();
    a1.Clave = 2;
    a1.Nombre = "Gato";
    lstAnimales.Add(a2);
    
    //Create Animal 3
    Animales a3 = new Animales();
    a1.Clave = 3;
    a1.Nombre = "Ratón";
    lstAnimales.Add(a3);

    //Additional Items below 

    Option 2 : Create a Constructor that will accepts your parameters and allow you to pass them in

    Using the following updated Constructor : 

    public class Animales
    {
         public int Clave { get; set;}
         public string Nombre { get; set; }
    
         //Empty Constructor
         public Animales()
         {
    
         }
    
         //Constructor that accepts your "Clave" and "Nombre" parameters
         public Animales(int clave, string nombre)
         {
             Clave = clave;
             Nombre = nombre;
         }
    }
    

    and then you could add your values individually like so : 

    //Create your List
    List<Animales> lstAnimales = new List<Animales>();
    
    //Create Animal 1
    lstAnimales.Add(new Animales(1,"Perro"));
    
    //Create Animal 2
    lstAnimales.Add(new Animales(2,"Gato"));
    
    //Create Animal 3
    lstAnimales.Add(new Animales(3,"Ratón"));
    
    //Additional Items below 

    Option 3 : Instantiate the List using your Items (similar to above)

    Another option rather than adding all of the items individually would be to simply create the List and automatically include all of these items : 

    //Create your List with your items already prepopulated (separated with commas)
    List<Animales> lstAnimales = new List<Animales>(){
                                          new Animales(1,"Perro"),
                                          new Animales(2,"Gato"),
                                          new Animales(3,"Ratón")
                                      };
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 11, 2013 1:41 PM