Tech Tip: Quick code snippet for locating an object property via its property path
PRODUCT: 4D | VERSION: 18 | PLATFORM: Mac & Win
Published On: June 18, 2020
In a situation where one needs to lookup an object property via a PropertyPath (variable) without knowing beforehand what that path would be, here is an approach that may be useful:
Take for example the following helper method, OBJ_lookupPath :
C_OBJECT($1) C_TEXT($2) C_VARIANT($0) $0:=Formula from string("this."+$2).call($1) |
This helper method above can be used to locate the value of a property path like this:
C_TEXT(vResult) vResult:=OBJ_lookupPath (ob;propertyPath) |
For example, given the following object:
C_OBJECT(ob) ob:=New object("a";New object("b";"c")) |
We could determine the value of a.b to equal c like this:
C_OBJECT(ob) ob:=New object("a";New object("b";"c")) C_TEXT(propertyPath) propertyPath:="a.b" C_TEXT(vResult) vResult:=OBJ_lookupPath (ob;propertyPath) // vResult = "c" |