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 name类的对象(名child Public为的类的实例)添加到名family为的集合。The 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. 若要查看其工作原理,请Form创建包含Button两个控件的Text ,并将List其属性设置为Add和。To see how this works, create a Form with two Button controls and set their Text properties to Add and List. 向窗体代码添加family 类定义和声明。childAdd the child class definition and the family declaration to the form code. 按如下所示修改 "添加" 和 "列表" 按钮的事件处理程序。_ClickModify 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.

适用于