Question MARK PONGONIS · Dec 15, 2020

Using a variable to reference an object property

Anyone know if it's possible using cache objectscript (or something else maybe) to reference an object property using a variable? For example:

s obj={}

s obj.prop1="hello"

s var="prop1"

w obj.var    <== which would result in a reference to obj.prop1 and write out "hello"

Product version: Caché 2017.1

Comments

Michael Clayton · Dec 15, 2020

The following will work for you:

w obj.%Get(var)

0
MARK PONGONIS  Dec 15, 2020 to Michael Clayton

works like a charm! Thanks!!

0
MARK PONGONIS  Dec 15, 2020 to Michael Clayton

Also works like a charm!!

0
Ben Spead  Dec 15, 2020 to tim huggins

No, no - *please* don't encourage the the use of eXecute ... that can make maintenance so very difficult :(  Most ObjectScript coding standards I have seen strictly forbid the use of this :) 

0
Tim Huggins  Dec 16, 2020 to Ben Spead

I'm not encouraging. I wouldn't use execute here but i would also encourage developers to know what execute is and why not to use it in most circumstances.

0