Tech Tip: Utility Method to Find Pointer in Collection
PRODUCT: 4D | VERSION: 18 | PLATFORM: Mac & Win
Published On: June 14, 2021
Pointers are not allowed to be passed as the search expression in collection.indexOf(). This is a utility method that finds the index position of the first found occurance of the pointer or pointer value in a collection. If no found occurances are found, it returns -1. The method accepts four parameters.
- $1 (Collection) = Accepts the collection to search
- $2 (Pointer or Variant) = Accecpts the pointer or pointer value to search for
- $3 (Longint) = Index to start the search at
- $4 (Longint) = Specify if searching for pointer or pointer value. Pass 1 to search pointer. Pass 2 to search pointer value
- $0 (Longint) = Returns the index position of the first matching pointer or pointer value. If no matches were found, returns -1
//------------------------------------ //NAME: Util_FindPtrInCol //DESCRIPTION: Method finds the first occurance of the matching pointer or pointer value in a collection // - $1 (Collection) = Accepts the collection to search // - $2 (Pointer or Variant) = Accecpts the pointer or pointer value // - $3 (Longint) = Index to start the search // - $4 (Longint) = Specify if searching for pointer or pointer value. Pass 1 to search pointer. Pass 2 to search pointer value // - $0 (Longint) = Returns the index position of the first matching pointer or pointer value. If no matches were found, returns -1 //------------------------------------ C_COLLECTION($1; $col) C_VARIANT($2; $item; $ptrContent) C_LONGINT($3; $4; $0; $start; $found; $i) C_POINTER($ptr) $col:=$1 $start:=$3 $found:=-1 Case of : ($4=1) //searches for pointer If (Value type($2)=Is pointer) $ptr:=$2 For ($i; $start; $col.length-1) If (Value type($col[$i])=Is pointer) If ($col[$i]=$ptr) //searches for pointer $found:=$i $i:=$col.length //break for loop End if End if End for End if : ($4=2) //searches for value of pointer $ptrContent:=$2 For ($i; $start; $col.length-1) If (Value type($col[$i])=Is pointer) If (Value type($col[$i]->)=Value type($ptrContent)) If ($col[$i]->=$ptrContent) //searches for content of pointer $found:=$i $i:=$col.length //break for loop End if End if End if End for End case $0:=$found |
Here is an example of calling this method.
C_TEXT($var) C_COLLECTION($col) $var:="my pointer content" $col:=New collection(0; 1; 2; 3; ->$var) $found:=Util_FindPtrInCol($col; ->$var; 0; 1) $found:=Util_FindPtrInCol($col; "my pointer content"; 0; 2) |