Code sample 1 to illustrate the data broker object:
**** Collection class in case you don't have one *** DEFINE CLASS foxcollection AS CUSTOM DIMENSION ITEM[1] COUNT = 0 currentindex = 0
PROCEDURE INIT() THIS.ITEM[1] = .NULL. ENDPROC
PROCEDURE getitems(tnindex) LOCAL uitem
*-- Do a bounds check on the passed parameter. *-- If the count = 0, no passed integer, positive, negative or zero, will test true. IF BETWEEN(tnindex,1,THIS.COUNT) uitem = THIS.ITEM[tnIndex] ELSE *-- Since we can have no knowledge of the datatype of the collection. *-- return a NULL if unable to comply with the request. uitem = .NULL. ENDIF
RETURN uitem
ENDPROC
PROCEDURE ADD(tuitem, tlfirst)
LOCAL lretval lretval = .F.
*-- Don't allow adding of nulls to the collection. IF !ISNULL(tuitem) THIS.COUNT = THIS.COUNT + 1
DIMENSION THIS.ITEM[this.Count]
IF tlfirst
*-- Insert object at the begining of the array. IF AINS(THIS.ITEM,1) = 1 THIS.ITEM[1] = tuitem lretval = .T.
ELSE THIS.COUNT = THIS.COUNT - 1 DIMENSION THIS.ITEM[this.Count] ENDIF ELSE *-- Stick 'er on the end of the array. THIS.ITEM[this.Count] = tuitem lretval = .T. ENDIF
ENDIF
ENDPROC
PROCEDURE SORT(tcobjproperty)
LOCAL ninc
*-- This is intended only for sorting objects based on a property. IF THIS.COUNT > 0 AND ; VARTYPE(THIS.ITEM[1]) = 'O' AND ; VARTYPE(tcobjproperty) = 'C' AND ; pemstatus(THIS.ITEM[1], tcobjproperty, 5)
LOCAL ARRAY atemp[this.Count,2]
*-- Build an array with a sortable column and the related object column. FOR ninc = 1 TO THIS.COUNT atemp[nInc, 1] = THIS.ITEM[nInc].&tcobjproperty atemp[nInc, 2] = THIS.ITEM[nInc] ENDFOR
*-- Sort on property IF ASORT(atemp,1,-1,1) = 1 FOR ninc = 1 TO THIS.COUNT *-- Re-fill the original array from the sorted array THIS.ITEM[nInc] = atemp[nInc, 2] ENDFOR ENDIF
ENDIF
ENDPROC
PROCEDURE REMOVE(tnindex) LOCAL lretval lretval = .F. IF TYPE("tnIndex") = "N" AND BETWEEN(tnindex,1,THIS.COUNT)
*-- This will remove the indexed array value and move the rest 'up'. ADEL(THIS.ITEM, tnindex)
*-- Reset the item count and remove last row unless there is only one row. THIS.COUNT = MAX(THIS.COUNT-1,1) DIMENSION THIS.ITEM[this.Count]
IF THIS.COUNT = 1 *-- A deletion of a row in a one row array leaves it .F. *-- This collection is designed to be used with objects. IF TYPE("this.item[1]") = "L"
*-- The item array must be empty. THIS.ITEM[1] = .NULL. THIS.COUNT = 0 ENDIF ENDIF
lretval = .T.
ENDIF
RETURN lretval
ENDPROC
PROCEDURE RESET() LOCAL ninc FOR ninc = THIS.COUNT TO 1 STEP -1 THIS.REMOVE(ninc) ENDFOR
THIS.COUNT = 0 ENDPROC
ENDDEFINE
©2002 Gary Foster |