tags:

views:

111

answers:

0

I have an existing class that has a List and I need to change this List to a BindingList but the class has a property that return a ReadOnlyCollection of this list. This is important that this list can only be modified inside this class. Now I have to change this List to a BindingList so I can get notified in another class when the list has changed. The only way I know in order to set DataBindingSource.DataSource to reference this list in another class is to provide a property that returns this BindingList but this will expose the list and lift the readonly capability. Is there a way to set DataBindingSource.DataSource in another class and prevent the list expose to other class. Below is the sample code

//Existing code
public class MessageManager
{
    List<Message> messageList = new List<Message>();

    public ReadOnlyCollection<Message> ReadonlyMessageList
    {
        get { return messageList.AsReadOnly(); }
    }

}

//Want to change to
public class MessageManager
{
    BindingList<Message> messageList = new BindingList<Message>();

    public BindingList<Message> Messages
    {
        get { return messageList; }
    }

}

//New class
public class Browser
{
    BindingSource source = new BindingSource();
    public Browser()
    {
        source.DataSource = Messages;
        source.ListChanged += new System.ComponentModel.ListChangedEventHandler(source_ListChanged);
    }


    private int messageCount = 0;
    void source_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
    {
        messageCount++;
    }
}

MessageManager is an existing class. Is there a way to bind to the messageList in Browser class without exposing the underline messageList?

Thanks.