how can I use the same object already created in form1 in form2

0

I have this class

 public class lista
{
   Nodo l;
   public lista()
   {
       l = null;

   }
   public void insertar(int elem)
   {
       if (l == null)
       {
           l = new Nodo(elem);
            aux = l;
        }
       else
       {
            aux = l;
           while (aux.prox != null) aux = aux.prox;
           Nodo p = new Nodo(elem);
           aux.prox = p;

       }

   }
    Nodo aux;
   public string mostrar()
   {

       //Nodo aux = l;
        string p = "";

       if(aux!=null)
        {
            p= aux.elem.ToString();
            aux = aux.prox;
        }
        return p;
        //
    }
}

from the form1 I load the list with the Insert procedure later in the form2 I want you to show them but when initializing the form2 another object is instantiated to be able to access the list and since it is another object this empty what I'm trying to do is access what I already have in the object of form1

    
asked by Rodrigo Torrico 03.02.2018 в 19:24
source

1 answer

2

There are different ways to pass data between forms.

One of the simplest is to do it through the constructor: create a constructor in the form Form2 that accepts an argument of type lista and pass it the object created in Form1 .

In the Form2 the constructor would stay:

public partial class Form2: Form
{

    public Form2(lista datos)
    {
        InitializeComponent();
    }

    .....

And in the Form1 you would create the instance of Form2 passing the object lista :

lista miLista = new lista();

// Código inicialización objeto milista
// .....

var newForm2 = new Form2(miLista);
newForm2.Show();
    
answered by 03.02.2018 / 19:33
source