Referencing Objects of the Same Name

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

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Referencing Objects of the Same Name

Post by SparkOut » Sun Dec 22, 2013 9:26 pm

Just to be clear, as has been intimated before in this thread, you can reference each control by its ordinal number - which is defined by the layer on which it resides.

field 1 is the field nearest to the lowest layer on the card. (ie the field nearest to layer 1)
field 2 is the field next nearest to layer 1

button 1 is the button nearest layer 1, etc etc

for example:

layer 1 has a button
layer 2 has a field
layer 3 has another field
layer 4 has another button

layer 1 = button 1
layer 4 = button 2
layer 2 = field 1
layer 3 = field 2

put "hello world" into field 2

will put the "hello world" value into the field second nearest to layer 1 on the card, regardless of what it is named. To reference a field or other object by name the objects should all have an appropriate name. Naming things consistently will mean you have the best of both worlds, a "robust" naming convention that lets you identify each object granularly, while always being able to loop through the objects according to ordinal (ie its layer relative to layer 1)

If, as you say you have been naming things "Line1", "Line2" then you can alternatively

repeat with i = 1 to the number of buttons of this card
if there is a button ("Line" & i) then
-- do stuff with button ("Line" & i)
end if
end repeat

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10332
Joined: Wed May 06, 2009 2:28 pm

Re: Referencing Objects of the Same Name

Post by dunbarx » Sun Dec 22, 2013 10:53 pm

Interesting idea about setting the object Label to a unique name that can later be references by running though the list of all objects. Though with Buttons, using the object's Label isn't practical. Couldn't the same thing be done by creating and referencing a Custom Property for each object?
You could use a custom property, but will still run into the same (modest) barrier I think you alluded to in the above quote. You can:

select btn 3, or
select btn "buttonName", or
select btn id idValue

but you cannot:

selet btn "labelReference" or
select btn "customPropertyReference"

You would have to run a loop that checks these properties and acts accordingly:

repeat with y = 1 to the number of buttons -- uses a native control reference
if the label of btn y = "someLabelInstance" then doStuff --uses a property reference
end repeat

Craig Newman

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Referencing Objects of the Same Name

Post by Simon » Sun Dec 22, 2013 10:55 pm

Wait a sec!
Line1, Line2, Line3 ... Line1

Code: Select all

put "hello world" into fld "test" 3
put "hello world" into fld "test"[3]
put "hello world" into fld 3 "test"
put "Hello World" into fld "field3"--yes that works

repeat with x=1 to 3
put "Hello World" into (line & x) of fld "field3" --I remembered this time :D
end repeat

Is that what you mean townsend?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Re: Referencing Objects of the Same Name

Post by townsend » Sun Dec 22, 2013 11:05 pm

Code: Select all

repeat with i = 1 to the number of buttons of this card
    if there is a button ("Line" & i) then
        -- do stuff with button ("Line" & i)
    end if
end repeat
Excellent!! That works!

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Referencing Objects of the Same Name

Post by Simon » Mon Dec 23, 2013 12:05 am

"Referencing Objects of the Same Name"
Wild goose...
"Referencing Objects with Similar Numerically Incremented Name"

Glad you got it working! :)

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Post Reply