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 charlesugo_1, 05-26-2026, 05:03 PM
      0 responses
      72 views
      0 likes
      Last Post charlesugo_1  
      Started by DannyP96, 05-18-2026, 02:38 PM
      1 response
      152 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 05-11-2026, 05:56 AM
      0 responses
      162 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 05-10-2026, 08:12 PM
      0 responses
      100 views
      0 likes
      Last Post CarlTrading  
      Started by Hwop38, 05-04-2026, 07:02 PM
      0 responses
      288 views
      0 likes
      Last Post Hwop38
      by Hwop38
       
      Working...
      X