0

I have 9 Forms, in every form i can fill List and i must write content of List to textBox. I tried to make public static List<Basket> sas = new List<Basket>(); in Basket.cs , but It doesn't help.

That how i try to output

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

I tried to do like here How to make a list of classes publicly accessible? , but it doesn't work. So, I just want to know how to use 1 List in different forms.

Roomey
  • 716
  • 2
  • 9
  • 16
  • Maybe [this Q&A](https://stackoverflow.com/q/38768737/3959259) helps you – Bill Tür stands with Ukraine Nov 15 '19 at 11:17
  • 2
    `sas` is a static member of the `Basket` class, so in order to access it from outside that class you would need to call it like so: `Basket.sas`, in your `foreach` you're currently trying to access a member of `Form1` called `sas`, which as far as we can tell from the information you have provided does not exist. – IAmJersh Nov 15 '19 at 11:29
  • TheHitchenator, God bless you and your family!!! It worked!!! – Roomey Nov 15 '19 at 11:31

3 Answers3

0

You could have your list in a central place. Creat your own class of Form that takes in the list as a constructor.

Then whenever using one of those forms, pass in the list from the central place in it's constructor.

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

Usage

var frm = new CustomForm(myGlobals.theList);
AntDC
  • 1,807
  • 14
  • 23
0

I just needed to add Basket before sas in foreach.

public Form1()
    {
        InitializeComponent();
        foreach(Basket e in Basket.sas)
        {
            basketBox.Text += e.Name + Environment.NewLine;
        }
    }
Roomey
  • 716
  • 2
  • 9
  • 16
-2

How about if you create a Session Variable in your first form?

You can do first:

Session["sas"]=List<Basket>;

And then in every form:

foreach(Basket e in (List<Carrito>)Session["sas"]) { basketBox.Text += e.Name + Environment.NewLine; }

If you dont use anymore:

Session.Remove("sas");