Collection.Remove Method |
Microsoft.VisualBasic.Collection
objectvariable .Remove ( index )
or:
objectvariable .Remove ( key )
An object variable of the Collection type
The ordinal position of the item to remove
The key of the item to remove
Removes a member from a collection
colMyCollection.Remove ("Name")
Members of the collection that follow the removed member are automatically moved downward by one ordinal position; therefore, no gaps are left in the collection.
Because the collection is reindexed after each deletion, you should be sure not to delete a member of the collection based on a stored numeric value of index , since this value could change. Instead, you should either delete the member by key or retrieve the index value just before calling the Remove method.
If you are deleting multiple members of a collection by numeric index value, you should delete them backwards from highest index value to lowest because the collection is reindexed after each deletion.
If you are using a collection as the basis for a class module, or if you are using functions in your application to wrap and enhance the limited functionality of a collection, you can include a Clear method to remove all the members in your collection. The method should be written to remove the member in position 1 until no members are left, as the following code demonstrates :
Public Sub Clear( ) Dim i As Integer For i = 1 To mcolMyCollection.Count mcolMyCollection.Remove(1) Next i End Sub
Alternately, you could do the same thing by working from the end of the collection forward, as the following code illustrates:
Dim intCtr As Integer For intCtr = objCollec.Count To 1 Step -1 objCollec.Remove(intCtr) Next
When using named arguments, providing an index value with the key:= keyword or providing a key name with the index:= keyword generates a runtime error.
Collection Class, Collection.Add Method, Collection.Count Property, Collection.Item Method