简体   繁体   中英

C# How to trigger an event when a static Arraylist count is changed?

I have a static array list in class called cart, is there a way to implement event listener to be used by other classes? I am new to c# tried to understand PropertyChangedEventHandler with no success. The arraylist and property are initialized as below:

private static ArrayList cartList = new ArrayList();

    public static ArrayList CartList
    {
        get { return cartList; }
        set
        {
            cartList = value;
        }
    }

Edit:

Altered @KMC code to work with List by hiding base methods:

public class ListOfProducts : List<Product>
{

    public void Add(Product obj)
    {
        base.Add(obj);
        this.OnChange();
    }

    public void UpdateLast(Product obj)
    {
        base[base.Count - 1] = obj;
        this.OnChange();
    }
    public void Remove(Product obj)
    {
        base.Remove(obj);
        this.OnChange();
    }

    public void Clear()
    {
        base.Clear();
        this.OnChange();
    }

    public event EventHandler Change;

    protected void OnChange()
    {
        if (this.Change != null)
        {
            this.Change(this, new EventArgs() { });
        }
    }
}

You can't get notified when count of elements in ArrayList changes nor when any element changes.

ArrayList and most other standard collections in .Net Framework like List<T> , Dictionary and arrays (except specialized classes like ObservableCollection in WPF) do not provide notification on items added/removed. You can easily check that by looking at MSDN entry for corresponding class and see if it implements INotifyCollectionChanged and/or INotifyPropertyChanged . Ie ArrayList only implements IList, ICollection, IEnumerable, ICloneable and no notification interfaces.

If you need notification of such change you should create your own class with such notification or use ObservableCollection .

Side note: please consider using List<T> instead of non-generic ArrayList - ArrayList vs List<> in C# .

the ArrayList type is extensible. you can override the add and remove methods and fire some events.

for ex)

public class NewArrayList : ArrayList 
{
    public event EventHandler Change; 

    public override int Add(object value)
    { 
       var result = base.Add(value);
        this.OnChange();
        return result;
    }

    public override void Remove(object obj)
    {
        base.Remove(obj);
        this.OnChange();
    }

    protected void OnChange()
    {
        if (this.Change != null)
        {
            this.Change(this, new EventArgs() { });
        }
    }
}
public static class program
{ 
    static void Main(string[] args)
    {

        var list = new NewArrayList();
        list.Change += delegate (object sender, EventArgs arg) {
            Console.WriteLine("collect changed {0}", list.Count);
        };

        list.Add(1);
        list.Add(2);
        list.Remove(2);

    }  

}

since the ArrayList needs to send some type of message (event) and I'm not sure who will be receiving it, I'll leave it up to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM