adam8
asked on
Erasing Collection
Hi,
I want to erase the contents of a collection but this doesn't work, just say TheCollection already stored 23 entrees
For X = 1 To TheCollection.Count
TheCollection.Delete(X)
Next X
Can someone give me code that would work
I want to erase the contents of a collection but this doesn't work, just say TheCollection already stored 23 entrees
For X = 1 To TheCollection.Count
TheCollection.Delete(X)
Next X
Can someone give me code that would work
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I think it is delete,
It doesn't matter anyway,
I know what you are talking about
It doesn't matter anyway,
I know what you are talking about
ASKER
it is very important that the entire contents of the collection is deleted.
The .Delete versus .Remove doesn't matter - it could be version differences. Just go with removing the first entry each time in your for...next loop, and you'll be fine.
The reason this will work:
Assume you have five items in your collection. When you delete the first item, the 2nd item is automatically renumbered to be the first item, the 3rd becomes the 2nd, etc.
Given a collection:
1 2 3 4 5
After deleting #1, your collection is:
1 2 3 4
After deleting #2, your collection is:
1 2 3
Etc.
Assume you have five items in your collection. When you delete the first item, the 2nd item is automatically renumbered to be the first item, the 3rd becomes the 2nd, etc.
Given a collection:
1 2 3 4 5
After deleting #1, your collection is:
1 2 3 4
After deleting #2, your collection is:
1 2 3
Etc.
ASKER
ok, i forgot to think about that.
so how would i go about deleting these items.
For X = 1 to Items.Count
Items.Delete(1)
Next X
Would that work?
so how would i go about deleting these items.
For X = 1 to Items.Count
Items.Delete(1)
Next X
Would that work?
That should be just perfect.
Hi
For collection it's better to use For Each statement:
Dim X As Variant, TheCollection As Collection
For Each X in TheCollection
TheCollection.Remove (1)
Next X
BTW, if your collection has delete method, may be it has clear method?
Cheers
For collection it's better to use For Each statement:
Dim X As Variant, TheCollection As Collection
For Each X in TheCollection
TheCollection.Remove (1)
Next X
BTW, if your collection has delete method, may be it has clear method?
Cheers
ASKER
it is delete, i just got mixed up.
The problem is that i store info to the collection until the user changes his/her selection, then if that happens i need to remove every item in the collection and then add different info to the collection the second time.
Get it, i need to remove all the items in the collection, not just the first one.