Collection.Remove 方法

定義

Collection 物件移除項目。Removes an element from a Collection object.

多載

Remove(Int32)

Collection 物件移除項目。Removes an element from a Collection object.

Remove(String)

Collection 物件移除項目。Removes an element from a Collection object.

Remove(Int32)

Collection 物件移除項目。Removes an element from a Collection object.

public:
 void Remove(int Index);
public void Remove (int Index);
member this.Remove : int -> unit
Public Sub Remove (Index As Integer)

參數

Index
Int32

數值運算式,其指定集合中項目的位置。A numeric expression that specifies the position of an element of the collection. Index 必須是從 1 到集合 Count 屬性值的一個數字。Index must be a number from 1 through the value of the collection's Count property.

範例

這個範例說明如何使用 Remove 方法,從變數 birthdays中的 Collection 物件移除物件。This example illustrates the use of the Remove method to remove objects from a Collection object in the variable birthdays.

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")

Add 方法的四個呼叫之後,Count 屬性包含4、元素 "Bill" 具有索引值1,而元素 "Pete" 的索引值為4。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.

在第一次呼叫 Remove之後,Count 為3、刪除元素 "Bill",而元素 "Pete" 的索引值為3。Following the first call to Remove, Count is 3, element "Bill" is deleted, and element "Pete" has index value 3.

在第二次呼叫 Remove之後,Count 為2,會刪除元素 "Mike",而元素 "Pete" 的索引值為2。Following the second call to Remove, Count is 2, element "Mike" is deleted, and element "Pete" has index value 2.

備註

Remove 從集合中刪除元素時,它會將集合的 Count 屬性遞減一。When Remove deletes an element from a collection, it decrements the collection's Count property by one. 它也會遞減先前遵循集合中已刪除之專案的每個元素的 Index 值。It also decrements the Index value of every element that formerly followed the deleted element in the collection.

如果在沒有 Key的情況下,將專案加入至集合,則必須使用其 Index 將它移除。If an element was added to the collection without a Key, you must use its Index to remove it.

Remove(String)

Collection 物件移除項目。Removes an element from a Collection object.

public:
 void Remove(System::String ^ Key);
public void Remove (string Key);
member this.Remove : string -> unit
Public Sub Remove (Key As String)

參數

Key
String

唯一的 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 必須對應至當項目新增至集合時所指定的 Key 引數。Key must correspond to the Key argument specified when the element was added to the collection.

範例

這個範例說明如何使用 Remove 方法,從變數 birthdays中的 Collection 物件移除物件。This example illustrates the use of the Remove method to remove objects from a Collection object in the variable birthdays.

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")

Add 方法的四個呼叫之後,Count 屬性包含4、元素 "Bill" 具有索引值1,而元素 "Pete" 的索引值為4。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.

在第一次呼叫 Remove之後,Count 為3、刪除元素 "Bill",而元素 "Pete" 的索引值為3。Following the first call to Remove, Count is 3, element "Bill" is deleted, and element "Pete" has index value 3.

在第二次呼叫 Remove之後,Count 為2,會刪除元素 "Mike",而元素 "Pete" 的索引值為2。Following the second call to Remove, Count is 2, element "Mike" is deleted, and element "Pete" has index value 2.

備註

Remove 從集合中刪除元素時,它會將集合的 Count 屬性遞減一。When Remove deletes an element from a collection, it decrements the collection's Count property by one. 它也會遞減先前遵循集合中已刪除之專案的每個元素的 Index 值。It also decrements the Index value of every element that formerly followed the deleted element in the collection.

如果在沒有 Key的情況下,將專案加入至集合,則必須使用其 Index 將它移除。If an element was added to the collection without a Key, you must use its Index to remove it.

適用於