Fill a list of objects in C #

0

Hello I want to fill a list with objects after consulting a bd,

List<EUbigeo> lista = new List<EUbigeo>();
        String query = "select * from ubigeo where estado='A'";
        comando.Connection = conexion.openConexion();
        comando.CommandText = query;
        leer = comando.ExecuteReader();
        EUbigeo ubigeo = new EUbigeo();
        try {

             while(leer.Read())
            {

                ubigeo.Departamento = Convert.ToString(leer[0]);
                ubigeo.Provincia = Convert.ToString(leer[1]);
                ubigeo.Distrito = Convert.ToString(leer[2]);
                ubigeo.Nombre= Convert.ToString(leer[3]);
                ubigeo.Estado = Convert.ToChar(leer[4]);
                lista.Add(ubigeo); 
                }

         }  
        catch (Exception e)
        {
            MessageBox.Show("Error: " + e.Message);
        }
        return lista;

now it turns out, that the list of objects is full but of the same object, that is if the last object is: ubigeo [departamento5, provincia5, distrito5, A] ... the complete list is full of that same object.

It has already been tested in the following way with the same result

String query = "select * from ubigeo where estado='A'";
        comando.Connection = conexion.openConexion();
        comando.CommandText = query;
        leer = comando.ExecuteReader();  
        try {

            while (leer.Read())
            {

                lista.Add(new EUbigeo()
                {
                    Departamento = Convert.ToString(leer[0]),
                    Provincia = Convert.ToString(leer[1]),
                    Distrito = Convert.ToString(leer[2]),
                    Nombre = Convert.ToString(leer[3]),
                    Estado = Convert.ToChar(leer[4])

                });
            }
         }  
        catch (Exception e)
        {
            MessageBox.Show("Error: " + e.Message);
        }
        return lista;
    
asked by Novato 25.05.2018 в 00:50
source

3 answers

1

Your problem is here:

EUbigeo ubigeo = new EUbigeo();
try {
    while(leer.Read())
    {
        ...
        lista.Add(ubigeo); 
    }
...

You have created a ubigeo object out of the cycle, so every time you add it to the list, you will be adding that same object, but with the updated information. The reason why you always have the same information, is that a reference to the object is stored every time you added it to the list, and not a new object.

Putting it into the cycle should correct your problem:

try {
    while(leer.Read())
    {
        EUbigeo ubigeo = new EUbigeo();
        ...
        lista.Add(ubigeo); 
    }
...
    
answered by 25.05.2018 в 01:00
0

I answer myself, with a question: My class was initially structured like this (this comment), that way it generated that the list was filled with the same object.

public class EUbigeo
{
    //private static String _departamento;
    //private static String _provincia;
    //private static String _distrito;
    //private static String _nombre;
    //private static char _estado;

    //public String Departamento
    //{
    //    get { return _departamento; }
    //    set { _departamento = value; }
    //}

and it was changed in the following way, and it works ok:

    public String Departamento { get; set; }
    public String Provincia { get; set; }
    public String Distrito { get; set; }
    public String Nombre { get; set; }

What is the difference in the statements? .. to be declared Static?

    
answered by 25.05.2018 в 04:02
0

That's because you're filling it with the same instance of the object, you're using the same position in memory.

usa

lista.Add(new EUbigeo(){
          Departamento = Convert.ToString(leer[0]),
          Provincia = Convert.ToString(leer[1]),Convert.ToString(leer[2]),
          Nombre= Convert.ToString(leer[3]),Estado = Convert.ToChar(leer[4])});
    
answered by 25.05.2018 в 01:01