Collection.Remove Method (String)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Removes an element from a Collection object.

Namespace:  Microsoft.VisualBasic
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

Syntax

'Declaration
Public Sub Remove ( _
    Key As String _
)
public void Remove(
    string Key
)

Parameters

  • Key
    Type: System.String
    A unique String expression that specifies a key string that can be used, instead of a positional index, to access an element of the collection. Key must correspond to the Key argument specified when the element was added to the collection.

Remarks

When Remove deletes an element from a collection, it decrements the collection's Count property by one. It also decrements the Index value of every element that formerly followed the deleted element in the collection.

If an element was added to the collection without a Key, you must use its Index to remove it.

Examples

This example illustrates the use of the Remove method to remove objects from a Collection object in the variable birthdays.

Dim Msg As String

Dim birthdays As New Collection()
birthdays.Add(New DateTime(2001, 1, 12), "Bill")
birthdays.Add(New DateTime(2001, 1, 13), "Joe")
birthdays.Add(New DateTime(2001, 1, 14), "Mike")
birthdays.Add(New DateTime(2001, 1, 15), "Pete")


...


birthdays.Remove(1)
birthdays.Remove("Mike")

Following the four calls to the Add method, the Count property contains 4, element "Bill" has index value 1, and element "Pete" has index value 4.

Following the first call to Remove, Count is 3, element "Bill" is deleted, and element "Pete" has index value 3.

Following the second call to Remove, Count is 2, element "Mike" is deleted, and element "Pete" has index value 2.

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.