Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access List from another class

can anyone tell me how to create a list in one class and access it from another?

like image 919
Brian Avatar asked Sep 15 '10 11:09

Brian


1 Answers

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.

like image 63
Mark Byers Avatar answered Oct 02 '22 23:10

Mark Byers