passing a field as an argument

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
mhsu
Posts: 8
Joined: Thu Aug 04, 2016 2:33 pm

passing a field as an argument

Post by mhsu » Mon Aug 29, 2016 5:54 pm

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?

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: passing a field as an argument

Post by FourthWorld » Mon Aug 29, 2016 6:06 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: passing a field as an argument

Post by Klaus » Mon Aug 29, 2016 6:09 pm

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

mhsu
Posts: 8
Joined: Thu Aug 04, 2016 2:33 pm

Re: passing a field as an argument

Post by mhsu » Mon Aug 29, 2016 6:43 pm

Awesome, thanks so much for the help!
Richard, that sounds like great advice regarding variable naming; I'll definitely adopt that convention.

Post Reply