Collection.Add(Object, String, Object, Object) 方法

定義

將項目新增至 Collection 物件。Adds an element to a Collection object.

public void Add (object Item, string Key = null, object Before = null, object After = null);
member this.Add : obj * string * obj * obj -> unit
Public Sub Add (Item As Object, Optional Key As String = null, Optional Before As Object = null, Optional After As Object = null)

參數

Item
Object

必要。Required. 任何型別的物件,其指定要新增到集合中的項目。An object of any type that specifies the element to add to the collection.

Key
String

選擇性。Optional. 唯一的 String 運算式,其指定用來存取集合中這個新項目的索引鍵字串,藉以取代位置索引。A unique String expression that specifies a key string that can be used instead of a positional index to access this new element in the collection.

Before
Object

選擇性。Optional. 運算式,其指定集合中的相對位置。An expression that specifies a relative position in the collection. 要新增的項目會放置在集合中由 Before 引數識別的項目之前。The element to be added is placed in the collection before the element identified by the Before argument. 如果 Before 為數值運算式,它必須是從 1 到集合 Count 屬性值的一個數字。If Before is a numeric expression, it must be a number from 1 through the value of the collection's Count property. 如果 BeforeString 運算式,當所參考的項目新增到集合中時,它必須對應到指定的索引鍵字串。If Before is a String expression, it must correspond to the key string specified when the element being referred to was added to the collection. 不能同時指定 BeforeAfterYou cannot specify both Before and After.

After
Object

選擇性。Optional. 運算式,其指定集合中的相對位置。An expression that specifies a relative position in the collection. 要新增的項目會放置在集合中由 After 引數識別的項目之後。The element to be added is placed in the collection after the element identified by the After argument. 如果 After 為數值運算式,它必須是從 1 到集合 Count 屬性值的一個數字。If After is a numeric expression, it must be a number from 1 through the value of the collection's Count property. 如果 AfterString 運算式,當所參考的項目新增到集合中時,它必須對應到指定的索引鍵字串。If After is a String expression, it must correspond to the key string specified when the element referred to was added to the collection. 不能同時指定 BeforeAfterYou cannot specify both Before and After.

範例

下列範例會使用 Add 方法,將 child 物件(稱為 child 包含 Public 屬性的類別實例)新增至名為 name 的集合。familyThe following example uses the Add method to add child objects - instances of a class called child containing a Public property name - to a collection called family. 若要查看其運作方式,請建立具有兩個 Button 控制項的 Form,並將其 Text 屬性設定為 AddListTo see how this works, create a Form with two Button controls and set their Text properties to Add and List. child 類別定義和 family 宣告新增至表單程式碼。Add the child class definition and the family declaration to the form code. 修改 [新增] 和 [清單] 按鈕的 _Click 事件處理常式,如下所示。Modify the _Click event handlers for the Add and List buttons as shown. [新增] 按鈕可讓您新增子系。The Add button allows you to add children. [清單] 按鈕會顯示所有子系的名稱。The List button displays the names of all the children.

Public Class child
    Public name As String
    Sub New(ByVal newName As String)
        name = newName
    End Sub
End Class
' Create a Collection object.
Private family As New Collection()
Private Sub addChild_Click() Handles Button1.Click
    Dim newName As String
    newName = InputBox("Name of new family member: ")
    If newName <> "" Then
        family.Add(New child(newName), newName)
    End If
End Sub
Private Sub listChild_Click() Handles Button2.Click
    For Each aChild As child In family
        MsgBox(aChild.name)
    Next
End Sub

備註

BeforeAfter 引數必須參考集合的現有元素。否則,就會發生錯誤。The Before or After argument must refer to an existing element of the collection; otherwise, an error occurs.

如果同時省略 BeforeAfter 引數,則會將新的物件加入至集合結尾。If both the Before and After arguments are omitted, the new object is added to the end of the collection.

如果指定的 Key 值符合集合中現有元素的索引鍵,也會發生錯誤。An error also occurs if a specified Key value matches the key for an existing element of the collection.

適用於