Click here to Skip to main content
12,761,078 members (31,681 online)
Rate this:
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)
        public event EventHandler<ListChangedEventArgs<T>> ListChanged;

        protected virtual void OnChanged(ListChangedEventArgs<T> e)
            if (ListChanged != null)
Posted 8-Oct-12 7:41am
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.

1 solution

Rate this: bad
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


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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 9 Oct 2012
Copyright © CodeProject, 1999-2017
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