Name
Collection.Remove Method
Class
Microsoft.VisualBasic.Collection
Syntax
objectvariable
.Remove (index
)
or:
objectvariable
.Remove (key
)
-
objectvariable
(required; Collection Object) An object variable of the Collection type
-
index
(required; Integer) The ordinal position of the item to remove
-
key
(required; String) The key of the item to remove
Description
Removes a member from a collection
Example
colMyCollection.Remove ("Name")
Programming Tips and Gotchas
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: ...
Get VB.NET Language in a Nutshell, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.