Home > Language Reference > Classes > Collection

Item Method

Retrieves an item from a collection.

Syntax

Public Function Item(ByVal vIndex As Integer) As Variant
Public Function Item(ByRef
vIndex As String) As Variant

ParameterDescription
vIndexIndex or key for the item to search for.

Remarks

The Item function returns the specified element from a collection. If the vIndex parameter is an Integer, the first flavor of the function is selected, and vIndex is treated as the one-based index of the element to be returned. On the other hand, if the vIndex parameter is a String, the second flavor of the function is selected, and vIndex is treated as the key of the element to be returned. In both cases, if the element could not be found, a runtime error occurs.

For example:

Private Sub Button1_Click()
  Dim c As New Collection

  c.Add 0.0,        "zero"
  c.Add 3.14159265, "pi"
  c.Add 2.71828182, "e"

  MsgBox c.Item(3)    ' Displays 2.71828182
  MsgBox c.Item("pi") ' Displays 3.14159265
End Sub

As with any other polymorphic function, the compiler selects the proper flavor of the Item function to call depending on the type of vIndex. Therefore, you should always pay attention when indexing a collection. For example:

Dim c As New Collection

Private Sub Button1_Click()
  MsgBox c.Item(Field1.Text)
  MsgBox c.Item(CInt(Field1.Text))
End Sub

The first line of code will treat the content of the text field as a key, while the second will treat the content of the text field as an index.

System requirements

SystemMinimal versionRemarks
Palm OSPalm OS 3.0N/A