Collection.Remove Method

   
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 :

     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.

See Also

Collection Class, Collection.Add Method, Collection.Count Property, Collection.Item Method

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net