Results 1 to 3 of 3

Thread: Removing Items from a Collection - Please Help

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2004
    Posts
    39

    Angry Removing Items from a Collection - Please Help

    Hi There

    Please see my code below:

    VB Code:
    1. Dim tiIndex As Integer
    2. For tiIndex = 0 To colTIData.Count - 1
    3.       If colTIData(tiIndex).tiKey = Id Then
    4.           colTIData.Remove(tiIndex)
    5.       End If  
    6.  Next

    I have 4 items in my collection. The first 2 items meet the condition, and I expect to have them removed, and at the end remain with 2.

    Now, when I run this code, Item 1 is removed & colTHdata.count becomes 3, but Item 2 is not. When tiIndex is 3, I get the following error:


    An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

    Additional information: Specified argument was out of the range of valid values.



    I can understand getting the error, since when I deleted Item1, Count was reduced from 4 to 3, and the statement:
    VB Code:
    1. For tiIndex = 0 To colTIData.Count - 1
    is referring to the original count of 4.

    To avoid the error, I had to include the following statement:
    VB Code:
    1. If tiIndex >= colTIData.Count - 1 Then
    2.                     Exit For
    3.                End If
    However, that doesn't solve my problem of wanting to remove 2 items

    How do I make sure that ALL ITEMS MEETING THE CONDITION ARE REMOVED FROM THE COLLECTION?

    Please help.

  2. #2
    New Member
    Join Date
    Dec 2003
    Posts
    12
    colTIData.Remove(colTIData.items.item(tiIndex)
    Things go better with rock

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2004
    Posts
    39
    Hi

    colTIData is a custom collection class, and your solution implies I have to implement the method "Items" How do I do that?

    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width