Function FieldPos() Foundation
Returns the position of a field in a work area.
FieldPos( <cFieldName> ) --> nFieldPosition
The return value of FieldPos() is an integer containing the position of the field named <cFieldName> within the work area. When <cFieldName> is not a valid field name in the work area, the value zero is returned.
The database function FieldPos() returns the position of an individual field within a record of a work area. If the function is used without the alias operator, the field position from the current work area is returned. The counterpart of FieldPos() is the function FieldName() which returns the name of the field in the record from the field position.
The function FieldPos() is frequently used with FieldGet() and FieldPut(). For example, this is done in programming generic read and write functions which operate without knowledge of the underlying data structure.
// The example demonstrates the most common use for FieldPos().
PROCEDURE Main
LOCAL cName
USE Customer NEW
? cLastName := FieldGet(FieldPos("LASTNAME")) // result: King
? FieldPos("LASTNAME") // result: 2
USE
RETURN
// In the example, a code block is assigned to a file-wide
// visible STATIC variable which can return the value of
// any field from any work area. The alias name of a work
// area and the desired field name must be passed to it as
// a character string.
STATIC sbReadField := ;
{ | cAlias, cFieldName | ; // parameter list for code block
( Select(cAlias) )-> ; // select work area
( FieldGet( FieldPos(cFieldName) ) ) ;
}
PROCEDURE Main
USE Customer ALIAS Cust NEW
USE Invoice ALIAS Inv NEW
USE InvItem ALIAS Items NEW
? Eval(sbReadField, "CUST", "LASTNAME") // result: King
? Eval(sbReadField, "INV" , "INVDATE") // result: 12/6/94
? Eval(sbReadField, "ITEMS", "Item") // result: Xbase++
CLOSE DATABASES
RETURN
// The example shows the UDF FieldStruct() which
// determines the structure of a field using FieldPos()
// and DbStruct().
PROCEDURE Main
USE Customer ALIAS Cust NEW
USE Invoice ALIAS Inv NEW
? FieldStruct("INVNO") // result: {"INVNO","C",6,0}
? Cust->(FieldStruct("LASTNAME")) // result: {"NAME","C",20,0}
CLOSE DATABASES
RETURN
FUNCTION FieldStruct(cFieldName)
LOCAL nFieldPos := FieldPos(cFieldName)
RETURN IIf(nFieldPos== 0,{}, DbStruct()[nFieldPos] )
If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.