JavaScript Editor js editor     Web development 



Main Page

Returns the key or index of an item in a collection depending on the value that is passed.

Collection.GetKey(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 of the collection.

  • String. The eIndex expression must correspond to the cKey that was specified for the item when it was added to the collection.

Return Value

The following table lists the return values for GetKey.

Return value Condition

String (key)

When passing an index or integer value

Integer (index)

When passing a key or string

Empty string ("")

When passing an index that does not exist or if items were added to the collection without keys

0

When passing a key that does not exist

Remarks

  • Because the GetKey method returns a value, you need to add a RETURN statement to the end of GetKey in the source code for any subclass you have modified. For example:

    В Copy Code
    RETURN DODEFAULT( eIndex )
    If you do not want to return the value, use the RETURN command without the DODEFAULT() function.

  • To determine if keys were specified when items were added to the collection, you can use the statement GetKey(1) and the EMPTY() function to check for an empty return value as follows:

    В Copy Code
    ? !EMPTY(Collection.GetKey(1))
  • To derive the index or key from an item in a collection from within a FOR EACH loop, use a normal FOR loop instead of a FOR EACH loop.

  • Visual FoxPro generates an error if the wrong type or no parameter is passed to GetKey.

Example

The following example illustrates the following tasks after creating a collection and adding items:

  • Check if items have keys.

  • Retrieve and display key for the second item.

В Copy Code
CLEAR
LOCAL oItems AS Collection

oItems = NEWOBJECT("Collection")
oItems.Add("Daffodils", "flower2")
oItems.Add("Roses", "flower1", "flower2")
oItems.Add("Daisies", "flower3")
? !EMPTY(oItems.GetKey(1))
? oItems.GetKey(2) 

See Also



JavaScript Editor js editor     Web development