0

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() { });
        }
    }
}
Convict Moody
  • 781
  • 1
  • 9
  • 28
  • 1
    You could implement INotifyProperty changed. See http://stackoverflow.com/questions/1315621/implementing-inotifypropertychanged-does-a-better-way-exist – Barry O'Kane Apr 19 '16 at 14:54
  • 2
    Could you be more specific? Do you want to receive events related to ArrayList itself or to each item contained in the list? – rudolf_franek Apr 19 '16 at 14:55
  • As a side note: Refrain from using ArrayList. Use List instead. Why? Because ArrayList is the old non-generic way of lists and stores all items as 'object'. List will store all items as a Cart object instead so you don't need to cast. Both more convenient and better performance. – Measurity Apr 19 '16 at 15:09
  • @rudolf_franek I want to recieve events for the change in number of of the contained items in the list. – Convict Moody Apr 19 '16 at 15:12

2 Answers2

2

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. I.e. 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#.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
2

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.

dfdsfdsfsdf
  • 653
  • 3
  • 7