Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Cant remove number from list: error message

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Cant remove number from list: error message

    Hello,

    For some reason i dont understand one item wont remove from my list.

    Code:
    int totalBarIndex = (ChartBars.ToIndex);
    
    var listP = new List<int>() {194,389,584,779,974,1169,1364,1559,1754,1949,2144 ,2339,2729,3119,3509,3899,4289,4679,5069};
    
    foreach(var list in listP)
    {
    if(list > totalBarIndex)
    {
    listP.Remove(194);
    listP.Remove(389);
    listP.Remove(584);
    listP.Remove(779);
    listP.Remove(974);
    listP.Remove(1169);
    listP.Remove(1364);
    listP.Remove(1559);
    listP.Remove(1754);
    listP.Remove(1949);
    listP.Remove(2144);
    listP.Remove(2339);
    listP.Remove(2729);
    listP.Remove(3119);
    listP.Remove(3509);
    listP.Remove(3899);
    listP.Remove(4289);
    listP.Remove(4679);
    listP.Remove(5069);
    }
    }
    Here's the result:

    I194
    I389
    I584
    I779
    I974
    I1169
    I1364
    I1559
    I1754
    Indicator 'MyCustomIndicator6': Error on calling 'OnRender' method on bar 1754: Collection was modified; enumeration operation may not execute.

    totalBarIndex = 1754

    Often it will return the number just after the value of totalBarIndex in this case 1949 and will not remove it from the list.

    Any idea why?

    TY

    #2
    Hello frankduc,

    Thank you for your post.

    You're trying to remove items on each iteration through the loop. So when it gets to a list item that is higher than the totalBarIndex, it loops through and removes all those from the list, then would go back to the top of the loop and try to iterate again but there's nothing left in the list, causing an error.

    If you try using break; to get out of the loop once you've removed all list items, do you get the error?

    Thanks in advance; I look forward to assisting you further.

    Comment


      #3
      You are right!
      Thank you Kate

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      571 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      330 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      101 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      548 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      548 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X