Page 1 of 1

passing a field as an argument

Posted: Mon Aug 29, 2016 5:54 pm
by mhsu
I want to take a field (name or id) as a parameter, and then write into that field. Currently the code looks like this:

Code: Select all

command writeOutput pField
     put "Hello" after pField
with the call being something like:

Code: Select all

put the long id of fld "test" into tTest
writeOutput tTest
However, this just updates the pField variable to have the long id of fld "test" followed by "Hello".
I've instead tried using the "do" command:

Code: Select all

command writeOutput pField
     do "put "&quote&"Hello"&quote&" after "&pField
which works, but seems ugly. Is there a better way to do this?

Re: passing a field as an argument

Posted: Mon Aug 29, 2016 6:06 pm
by FourthWorld
Do is a great solution when no other solution is available. Whenever I find myself tempted to use it, I consider it a yellow warning flag that I may have missed an opportunity to do the task more directly. Fortunately a more direct task is available here, setting the text property of the field:

Code: Select all

command writeOutput pFieldObj
     set the text of pFieldObj to "Hello"
end writeOutput
Stylistic note: When I use object references as arguments to a handler I often specify whether I'm passing in a complete object reference or just its name. When passing in something like the long ID I tend to append the variable name with "obj" so I know I'm getting a complete object reference. When passing in just the name I tend to append with "name" (e.g. pFieldName). This is a small things and won't matter much in a single use, but after writing thousands of handlers over many years I've found this modest habit has saved me some time when I'm down deep into the handler definition.

Re: passing a field as an argument

Posted: Mon Aug 29, 2016 6:09 pm
by Klaus
Hi mshu,

1. welcome to the forum! :D

2. When accessing fields like this you better set its TEXT property like this:

Code: Select all

command writeOutput pField
     set the TEXT of pField to (the text of pField && "Hello")
end writeOutput
Best

Klaus

Re: passing a field as an argument

Posted: Mon Aug 29, 2016 6:43 pm
by mhsu
Awesome, thanks so much for the help!
Richard, that sounds like great advice regarding variable naming; I'll definitely adopt that convention.