Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hello I'm trying to build my own custom collection that will resemble the ObservableCollection very much. I would like some help in firing the event. I just want to see if it works. What should I put in the OnChanged method and in the main window of the application?
 

My classes looks like this:
 
public class ListChangedEventArgs<T> : EventArgs
    {
        public ListChangedEventArgs(Operation operation, T value, int count);
 
        public int Count { get; protected set; }
        public Operation Operation { get; protected set; }
        public T Value { get; protected set; }
    }
 
 public class ObservableList<T> : IEnumerable<T>
    {
 
        private List<T> _internalList;
 
        public ObservableList()
        {
            _internalList = new List<T>();
        }
        
        public void Add(T item)
        {
            _internalList.Add(item);
        }
          
        public event EventHandler<ListChangedEventArgs<T>> ListChanged;
 
        protected virtual void OnChanged(ListChangedEventArgs<T> e)
        {
            if (ListChanged != null)
            {
                     ???
            }
        }
Posted 8-Oct-12 6:41am
Flum021430
Comments
Sergey Alexandrovich Kryukov at 8-Oct-12 15:10pm
   
You almost got it right, so I don't understand how come you need something else. So, what to do? I voted 4 for the question and answered, please see.
--SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

First, you probably need to change the access modifier of the constructor ListChangedEventArgs from public to protected.
I don't think you want to invoke the event from some other assembly, because you can invoke the event only in the class or structure where this event is declared, and this is most likely should be in the same assembly as ListChangedEventArgs definition; and because this constructor is only needed to invoke the event. You should not give more access then it's really needed.
 
Now, you need to add the code for invocation:
public class ObservableList<T> : IEnumerable<T> {
 
//...

    protected virtual void OnChanged(ListChangedEventArgs<T> e) {
        if (ListChanged != null) // that like was correct and important, but no need for the block
            Operation operation = //...
            T value = //...
            int count = //... you know better what to assign
            ListChanged.Invoke(this, new ListChangeEventArgs<T>(operation, value, count));
    } //OnChanged

} //class ObservableList
 
—SA
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 468
1 Sergey Alexandrovich Kryukov 305
2 PhilLenoir 115
3 RajeeshMenoth 110
4 Peter Leow 100
0 Sergey Alexandrovich Kryukov 6,557
1 OriginalGriff 5,966
2 CPallini 2,473
3 Richard MacCutchan 1,687
4 Abhinav S 1,540


Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 9 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100