O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required