can anyone tell me how to create a list in one class and access it from another?
To create a list call the list constructor:
class Foo
{
private List<Item> myList = new List<Item>();
}
To make it accessible to other classes add a public property which exposes it.
class Foo
{
private List<Item> myList = new List<Item();
public List<Item> MyList
{
get { return myList; }
}
}
To access the list from another class you need to have a reference to an object of type Foo
. Assuming that you have such a reference and it is called foo
then you can write foo.MyList
to access the list.
You might want to be careful about exposing Lists directly. If you only need to allow read only access consider exposing a ReadOnlyCollection
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With