|
-
May 18th, 2004, 03:39 AM
#1
Thread Starter
Hyperactive Member
Collection
Hey,
I used collection, to make object list.
dim v as new collection
I add items using key.
v.add(object, key)
Key is unique integer.
After I add (or replace) item, I need to remove all other items, which key is bigger.
How to do this?
Maybe I should use other class (not collection)?
-
May 18th, 2004, 03:58 AM
#2
Retired VBF Adm1nistrator

VB Code:
''declare the collection
''
Dim myColl As New Collection()
''add items to it
''
Dim i As Integer
For i = 0 To 99
myColl.Add(i, i)
Next
''now remove any items where key > intSomeNumber
''
Dim intSomeNumber As Integer = 67
Dim myItem As New Object(), blnClear As Boolean = False
Do Until blnClear
blnClear = True
For Each myItem In myColl
If myItem > intSomeNumber Then
myColl.Remove("" & myItem)
Debug.WriteLine("Remove : " & myItem)
blnClear = False
End If
Next
Loop
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
May 18th, 2004, 04:10 AM
#3
What is it about the blnClear boolean that makes it necessary in this loop?
Does it have something to do with the size of the collection decreasing after each Remove method?
-
May 18th, 2004, 04:20 AM
#4
Retired VBF Adm1nistrator
Originally posted by mendhak
What is it about the blnClear boolean that makes it necessary in this loop?
Does it have something to do with the size of the collection decreasing after each Remove method?
Yeah it appears to cache the size of the collection at the start of the loop - so that after a few iterations it's already past the end of the collection...
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
May 18th, 2004, 04:31 AM
#5
Which is why that loop must run several times.
Ok, Thanks.
-
May 18th, 2004, 04:46 AM
#6
Thread Starter
Hyperactive Member
Maybe this code would run faster?
VB Code:
Dim vv As New ArrayList
Dim i As Integer
For i = 0 To 99
vv.Add(i, i)
Next
'now remove any items where index > removeFrom
Dim removeFrom As Integer = 67
vv.RemoveRange(removeFrom, vv.Count-removeFrom)
Last edited by Norkis; May 18th, 2004 at 04:50 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|