Use a single list for several forms in C #

0

Good day experts, I am trying to use a list with four properties that can be accessed and modified by two different forms. The fields are the following:

    public class Articulos
{
    public String articulo { get; set; }
    public int cantidadSolicitud { get; set; }
    public int cantidadTomada { get; set; }
    public String lote { get; set; }
}

And the class where the collection is:

    public class ColeccionArticulos
{
    //La colección contendrá la lista de artículos de la solicitud
    public static List<Articulos> articulos;

    public ColeccionArticulos()
    {
        //Creamos una lista vacía
        articulos = new List<Articulos>();
    }

    //Se agrega un nuevo artículo a la lista
    public void agregarArticulo(Articulos nuevoArticulo)
    {
        articulos.Add(nuevoArticulo);
    }
}

In the form2 is where I create the instance:

    public partial class Lotes : Form
{
    public delegate void pasar(string dato);
    public event pasar pasado;
    Articulos a = new Articulos();
    ColeccionArticulos articulos = new ColeccionArticulos();

And in a click event command call the following function:

public void ActualizarRegistro()
    {
        int loop = 0;
        foreach (DataGridViewRow row in gridLotesDisponibles.Rows)
        {
            if (Convert.ToString(row.Cells[0].Value) != "")
                    {
                        a.articulo = lblArticulo.Text.ToString();
                        a.cantidadSolicitud = Int32.Parse(txtCantidadRequerida.Text.ToString(), NumberStyles.AllowThousands);
                        a.cantidadTomada = Int32.Parse(gridLotesDisponibles.Rows[loop].Cells[0].Value.ToString(), NumberStyles.AllowThousands);
                        a.lote = gridLotesDisponibles.Rows[loop].Cells[1].Value.ToString();

                        ColeccionArticulos.articulos.Add(a);
                    }
            loop++;
        }
        pasado("Ventana generada por interfaz");
    }

I have two problems that I have not been able to solve, the first is that within "articles" the DGV is traversed, however, only the data of the last row is stored, that is, when going through the following grid:

Only the last line is saved three times:

The second problem I have is that when you close the second window, press "Select lots" of another product and fill in the information of the corresponding batches, new information is stored but following the previous example, the 3 elements that already are created are deleted and do not leave them stored.

I do not know what I'm doing wrong on both points. I request your support to see if they can advise me something, guide me, or suggest an example or way to solve it.

Thanks in advance, best regards!

    
asked by Luis Villarreal 30.08.2017 в 23:03
source

2 answers

0

I recommend that you review this implementation of the Singleton model to ensure proper operation:

public class ColeccionArticulos
{
    private static ColeccionArticulos instance;

    public static ColeccionArticulos Instance
    {
        get 
        {
            if (instance == null)
                instance = new ColeccionArticulos ();
            return instance; 
        }
    }
    //La colección contendrá la lista de artículos de la solicitud
    private List<Articulo> articulos;

    public ColeccionArticulos()
    {           
        nuevaLista ();
    }

    // Un método para iniciar una nueva lista cuando lo necesites.
    public void nuevaLista()
    {
        //Creamos una lista vacía
        articulos = new List<Articulo>();
    }

    //Se agrega un nuevo artículo a la lista
    public void agregarArticulo(Articulo nuevoArticulo)
    {
        articulos.Add(nuevoArticulo);
    }
}

This will allow you to use it in this way:

ColeccionArticulos.Instance.agregarArticulo ( unArticulo );

When you need to start with a new list you just have to put:

    ColeccionArticulos.Instance.nuevaLista();

and when you need to obtain the values:

    Lista<Articulo> lista = ColeccionArticulos.Instance.getLista(); 

Important! Check to create a new object before adding it to the list.

I hope it serves you. Greetings!

    
answered by 30.08.2017 / 23:37
source
0

Good, the first mistake I see in your code is that you do not create a new article every time you add one, you're reusing the article to, so you're probably adding 3 articles with the same reference (that's why the value of the last article remains) :

public void ActualizarRegistro()
    {
        int loop = 0;
        foreach (DataGridViewRow row in gridLotesDisponibles.Rows)
        {
            if (Convert.ToString(row.Cells[0].Value) != "")
                    {
                        Articulos a = new Articulos(); //creando aquí cada artículo se solucionaría el problema
                        a.articulo = lblArticulo.Text.ToString();
                        a.cantidadSolicitud = Int32.Parse(txtCantidadRequerida.Text.ToString(), NumberStyles.AllowThousands);
                        a.cantidadTomada = Int32.Parse(gridLotesDisponibles.Rows[loop].Cells[0].Value.ToString(), NumberStyles.AllowThousands);
                        a.lote = gridLotesDisponibles.Rows[loop].Cells[1].Value.ToString();

                        ColeccionArticulos.articulos.Add(a);
                    }
            loop++;
        }
        pasado("Ventana generada por interfaz");
    }

Probably both failures have to do with this.

    
answered by 30.08.2017 в 23:13