Collection.Add Method

Adds an element to a Collection object.

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

public void Add(
	Object Item,
	string Key,
	Object Before,
	Object After
)

Parameters

Item
Type: System.Object

Required. An object of any type that specifies the element to add to the collection.

Key
Type: System.String

Optional. 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
Type: System.Object

Optional. An expression that specifies a relative position in the collection. The element to be added is placed in the collection before the element identified by the Before argument. If Before is a numeric expression, it must be a number from 1 through the value of the collection's Count property. 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. You cannot specify both Before and After.

After
Type: System.Object

Optional. An expression that specifies a relative position in the collection. The element to be added is placed in the collection after the element identified by the After argument. If After is a numeric expression, it must be a number from 1 through the value of the collection's Count property. If After is a String expression, it must correspond to the key string specified when the element referred to was added to the collection. You cannot specify both Before and After.

The Before or After argument must refer to an existing element of the collection; otherwise, an error occurs.

If both the Before and After arguments are omitted, the new object is added to the end of the collection.

An error also occurs if a specified Key value matches the key for an existing element of the collection.

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. To see how this works, create a Form with two Button controls and set their Text properties to Add and List. Add the child class definition and the family declaration to the form code. 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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft