Name

Collection.Add Method

Class

Microsoft.VisualBasic.Collection

Syntax

                  objectvariable.Add item [, key, before, after]
objectvariable (required; Collection Object)

The name of the Collection object to which an item is to be added

item (required; Object)

An object of any type that specifies the member to add to the collection

key (optional; String)

A unique string expression that specifies a key string that can be used, instead of a positional index, to access a member of the collection

before (optional; Object)

The member to be added placed in the collection before the member identified by the before argument (more on this in Section )

after (optional; Object)

The member to be added placed in the collection after the member identified by the after argument (more on this in Section )

Description

Adds an object to a collection

Rules at a Glance

  • If you do not specify a before or after value, the member is appended to the end of the collection (in index order).

  • If you do not specify a key value, you cannot access this member using a key, but instead must access it either by using its ordinal number or by enumerating all the members of the collection with the For Each...Next construct. Thus, keys are highly recommended.

  • The before or after argument can refer to an index or a key. For instance, consider the following code:

    Dim c As New Collection(  )
    c.Add("donna", "111")
    c.Add("steve", "222")
    'c.Add("bill", "333", "222")
    'c.Add("bill", "333", 2)
    MsgBox(c.Item(2))

    Both of the commented lines ...

Get VB.NET Language in a Nutshell, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.