Log In    |    Knowledge Base    |    4D Home
Tech Tip: The 4D Open APIs - Part IX: Selection to array (sample code)
PRODUCT: 4D Open | VERSION: 6.7 | PLATFORM: Mac & Win
Published On: September 7, 2001

The following example shows how to copy the contents of the current selection into an array; specifically, this code will copy the contents of five alphanumeric fields into an array and display them.

void SelectionToArray(void)
 ReqFieldRecPtr FieldList;
 ArrayHandle hBuffer;
 int2 TargetFile=1, n=9;
 int4 CountRecords;
 int myErr;
 Ptr pSrc;
 DataRec Data;
 short i;
 if ( FieldList == nil)
  myErr = MemError();
 Check(_4D_SelectAllRecords(hConnect, 1),"_4D_SelectAllRecords\0");
  FieldList = (ReqFieldRecPtr)malloc(sizeof(ReqFieldRec) + ((2*n)-1)*sizeof(int2));
  FieldList->NB_Fields = n;
  FieldList->TargetFile =TargetFile;
  FieldList->Fields[0]= TargetFile;
  FieldList->Fields[1]=1 ;
  FieldList->Fields[2]= TargetFile;
  FieldList->Fields[3]=2 ;
  FieldList->Fields[4]= TargetFile;
  FieldList->Fields[5]=3 ;
  FieldList->Fields[6]= TargetFile;
  FieldList->Fields[7]=4 ;
  FieldList->Fields[8]= TargetFile;
  FieldList->Fields[9]=5 ;
  hBuffer = nil;
  Check(_4D_SelectionToArray(hConnect, FieldList, &hBuffer), 36);
  CountRecords = **(long **)hBuffer;
  HLock((Handle) hBuffer);
  pSrc =*((Handle)hBuffer)+4 ;
  Data.typ = -1;
  for( i=0; i  pSrc +=pSrc[0]+4;
  BlockMove(pSrc, Data.u.s , pSrc[0]+5);
  Data.typ =s_AlphaNumeric;
  if ( hBuffer != nil)
  Free4DHandle((Handle4D) hBuffer);