0

I have List<Basket> sas = new List<Basket>(); in Form1. And i need to output all values from this List in another form.

Edited: Now i have problem with outputing.

    List<Basket> sas = new List<Basket>();
    public Form1()
    {
        InitializeComponent();
        foreach(Basket e in sas)
        {
            basketBox.Text += e.Name + Environment.NewLine;
        }
    }

I added elements in Form2, but when I try to output them in Form1 nothing happened.

Roomey
  • 716
  • 2
  • 9
  • 16

2 Answers2

1

There are several ways:

You can have the list as a static (global) variable in the form1 class and then use it in the other form.

Another solution would be to have a context class to be given to the other form when you construct it.

farbiondriven
  • 2,450
  • 2
  • 15
  • 31
1

if by from you mean windows forms, then you can simply make your list public static, so you can have access to it everywhere. If you are working with ASP.NET web forms then you probably should relay on your database.


PS: you can use the Singelton pattern or DI as well. but for the sake of simplicity stick with statics

F_IVI_L
  • 940
  • 9
  • 17