Item Method (Collection Class)
Returns a specific member object in the collection by numeric position or string key.
The member object can have any valid type that can be assigned to a memory variable. This includes simple data types, such as strings, numbers, dates, logical, or more complex ones such as Visual FoxPro and Component Object Model (COM) objects. The Item method for collection objects is typically used with FOR EACH...ENDFOR enumerations to reference members of the collection.
Collection.Item( eIndex )
Parameters
eIndex
Specifies a required expression that represents a position of an item in the collection. This expression can be one of two types:Numeric. The eIndex expression must have a value from 1 to the value of the Count property for the collection.
String. The eIndex expression must correspond to the cKey that was specified for the item when it was added to the collection.
If eIndex does not match an existing member of the collection, an error occurs.
Return Value
Returns the specific item.
Remarks
If an incorrect type is passed for a specific parameter, an error occurs.
The Item method for collection objects returns a value, which is the item itself. Therefore, you need to add a RETURN statement to the end of the Item method in the source code for any subclass you have modified:
RETURN DODEFAULT(eIndex)
If you do not want to return the item, use the RETURN statement without the DODEFAULT() function.
The Item method for collection objects is the default method for a collection. For example, suppose you have the following code:
Public MyCollection AS Collection
MyCollection = CREATEOBJECT("Collection")
MyCollection.Add(CREATEOBJECT("Form"),"myKey")
The following lines of code are equivalent:
? MyCollection(1)
? MyCollection.Item(1)
? MyCollection("myKey")
Example
The following example illustrates these tasks at run time:
Creates forms and a collection.
Adds forms to the collection.
Displays the name of each form in the collection.
Displays the number of forms in the collection.
Displays the name of the first item in the collection.
Displays the name of the first item in the collection using the Item method for collections.
loForm1 = CREATEOBJECT("myForm1")
loForm2 = CREATEOBJECT("myForm2")
loCol = CREATEOBJECT("myCollection")
loCol.Add(loForm1)
loCol.Add(loForm2)
? loCol(1).Name
? loCol.Item(1).Name
See Also
Reference
Collection Object Properties, Methods, and Events
Collection Class
FOR EACH ... ENDFOR Command
RETURN Command
DODEFAULT( ) Function