none
Trigger a function after a List element is added RRS feed

  • Question

  • I keep looking for a way of triggering a function in my code only after a List element is added, but i can't find any info to help me get this done.

    I've tried ObservableCollection as below, but the code doesn't trigger

    If anybody could help me find the error in my code, I will appreciate

    private List<int>       ListCountL;
    ...
    ListCountL = new List<int>();
    ...
    if (!LSwDmiAdd && b > 1 && LSwDMI[b-1] - LSwDMI[b] > 0.001 && LSwDMI[b-1] - LSwDMI[b-2] > 0.001)
    {
      LastLSwDMI.Add(LSwDMI[b-1]);
      listCountLL = LastLSwDMI.Count;
      ListCountL.Add(listCountLL);   
      j++;
    }
    ...
    if (ListCountL[j-1] > ListCountL[j-2]) 
    {
      // Logic triggered after ListCountL increase
    }                          


    var colL = new ObservableCollection<int>(ListCountL); colL.CollectionChanged += (s, e) => { if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) // This code never triggers, but in the previous code it did after // if (ListCountL[j-1] > ListCountL[j-2]) if (k > 1 && c > 1 && (lowSwBarDiffA <= BarDiff || lowSwBarDiffB <= BarDiff)) { if (!posDiv && ((LastLSwDMI[k-1] - LastLSwDMI[k-2] > 0.001 && LastLSwDMIprice[k-2] - LastLSwDMIprice[k-1] > TickSize) || (LSwDMI[c-1] - LastLSwDMI[k-1] > 0.001 && LastLSwDMIprice[k-1] - LSwDMIprice[c-1] > TickSize))) posDiv = true; else posDiv = false; } };


    Sunday, January 19, 2020 10:41 AM

All replies

  • your code makes is look as if you only subscribe to the CollectionChanged after you already added all elements int the ObservableCollection constructor.
    The event will of course only be fired for elements added after subscribing
    Sunday, January 19, 2020 11:32 AM
  • Hi itrader,

    Thank you for posting here.

    According to your description, you want to trigger an event when the List element changes.

    You can use "BindingList" to do this, the following is a code example.

            static void Main(string[] args)
            {
                BindingList<string> listOfParts = new BindingList<string>();
                listOfParts.ListChanged += new ListChangedEventHandler(listOfParts_ListChanged);
                for (int i = 0; i < 10; i++)
                {
                    listOfParts.Add("test" + i);
                }
    
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
            }
            private static void listOfParts_ListChanged(object sender, ListChangedEventArgs e)
            {
                Console.WriteLine("Change");
            }

    Result:

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 20, 2020 3:03 AM
  • Hi,

    Has your problem been solved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 4, 2020 8:38 AM
  • ObservableCollection is the correct approach if you want to get notified a collection has changed. Technically any collection that implements INotifyCollectionChanged would work but ObservableCollection is the one built into the framework.

    In the first block of code you posted I don't see any usage of ObservableCollection so honestly I don't know what you're expecting. In the second block of code you posted the creation of a collection and hooking up the event but since nothing gets added it doesn't matter. Note that `ListCountL` is not observable and therefore has nothing to do with this discussion. It doesn't matter that you used it to initialize `colL`. Only changes to `colL` would trigger the event.

    var colL = new Observablecollection<int>();
    colL.CollectionChanged += ...;
    
    //Triggers
    colL.Add(10);
    colL.Remove(20);
    
    Note also that changing properties on elements inside the array (not an issue here with the primitive int) won't trigger a notification either. Only changes to the collection itself do.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, February 4, 2020 2:43 PM
    Moderator