Duplicate control names

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
Traxgeek
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Wed Jan 09, 2013 10:11 am

Duplicate control names

Post by Traxgeek » Tue Sep 03, 2013 9:35 am

Hi all.

I've hit a problem and am keen to understand some of the more obscure 'rules' of labeling controls within LC.

Imagine if you will, a card "crd1" with a control "ctrl1" (very original ;-) ) on it

I then add a group "grp1" containing a control "ctrl1" (same name as the control on the card BUT, this control is in the group).

99% of the time I can put whatever data I like in whichever control I like simply by adding or omitting 'of group "grp1" ' after specifiying the recipient control

i.e. : set the text of field "ctrl1" to "Card level field"
or : set the text of field "ctrl1" of group "grp1" to "Group level field"

and this works... BUT... recently, on more than one occasion, this has not worked and LC seems to 'mix up' / 'become confused' as to which control I mean to reference with the 'set the text of field "ctrl1" to "Card level field" ' command line.
Like I say, 99% of the time LC understands what I mean simply by my adding / omiting the 'of group...' syntax.

By '99% of the time', it would be more accurate to state that, once LC has decided to reference the group control even though I don't add the 'of group...' syntax, it will then do so forever - each and every time the code line executes. LC does not change its mind once its made up. This is true even following an SDK / computer reboot...

If, I trap the error / insert a breakpoint immediately after the offending line and then message box : 'put the ID of the field "ctrl1"' (note, no ' of group...') I get one ID (764586)
but clicking on the control I get another ID (584239)
then if I message box : 'put the ID of the field "ctrl1" of group "grp1" ' I get the same ID as that without the 'of group...' line (764586)

I guess I've ;
-1- not fully read / understood the nuances of control naming within LC and
-2- gotten away with it for a long time (looks like I may have to go and change quite a lot of code)
-3- I'm unsure as to how LC 'finds'/'matches' the control I'm referencing with my friendly name ( 'ctrl1' ) to its numeric ID (764586). Hadn't given it a thought until now...

Would someone enlighten me on control naming conventions with LC please and how (apart from renaming duplicate control names preferably) I can get around this issue.

Like I say, this occurs only occasionally; sometimes I can 'fix' the issue by creating a new control and then deleting the 'faulty' one... sometimes not - it's a very hard 'bug' / issue to recreate and therefore track down. I'm not sure if copying groups of controls from one card to another is my problem creator or not - sometimes a data entry control set (group) on one card is similar to that on another card, so I've copied, pasted and then modified an existing group from a different card to suit a slightly different (but similar) purpose on a new card. Maybe that's just 'asking' for problems.

Hope someone can kick me in the right direction.

Thanks a million.
<EDIT>
In fact, typing ' put the ID of field "ctrl1" of card "crd1" ' produces the same result (764586) as ' put the ID of field "ctrl1" of group "grp1" ' (764586)
whereas, if I type ' put the ID of field "ctrl2" of card "crd1" ' produces 782419 and a different result for ' put the ID of field "ctrl2" of group "grp1" ' (839126)
To me (a noobie to LC) this is a strange result... in that it does not appear to be repetetive / common...

Someone else must have experienced this and (hopefully) found a (simple/non-destructive) way around it... ? ! ? hopefully
Ideas ?
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Duplicate control names

Post by MaxV » Tue Sep 03, 2013 10:52 am

My experience is this: watch out to use the same name.
Livecode stops at the first control with that name. The ID and the long ID of a control may help you. Try to use the long ID.
Use them instead of the names.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

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

Re: Duplicate control names

Post by Klaus » Tue Sep 03, 2013 12:30 pm

Hi Traxgeek,

you are right, using controls with the same name on a card is ASKING FOR TROUBLE! :D
Livecode cannot differ them and you couldn't either from only looking at the names! 8)

If this happens, Livecode usually refers to the FIRST (lowest layer) control with that name,
whatever that may be, so you should get used to give UNIQUE names to your control if you
want to address them via script.

Sorry, no other way to avoid these inconveniences.


Best

Klaus

Traxgeek
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Wed Jan 09, 2013 10:11 am

Re: Duplicate control names

Post by Traxgeek » Tue Sep 03, 2013 1:18 pm

Thanks both.

I had (obviously) expected issues if I were to try and create multiple controls with the same name on the same 'level'. I had (incorrectly) assumed (I come from other languages) that having a duplicate control name within a different 'level' or 'container' (like different groups on a card) would be OK simply because one could differentiate between them using the 'of group "MyGroupName" ' syntax.

Additionally, LC didn't throw up any compile errors and so far (I have done this with 4 common controls over 10+ cards) has worked flawlessly. I'd just assumed that the '... of group "MyGroup" ' would behave similarly to the '... of card "MyCard" ' style of syntax - obviously not !

I'm 'with you though' and did think it 'a liitle odd' that LC grouped my multiple control instances together in the Controls list section of the App Browser window and clicking on each item in the App Browser window ALWAYS highlights the correct control...

So, not trying to be picky or anything - just tidying up and (saving myself the effort of having to go and try) when you guys already know :
-1- if I have a control in a group (both uniquely named) do I HAVE to use the '... of group "MyGroup" ' syntax or not ?
-2- duplicating a control name on a different card is perfectly acceptable ?
-3- if ALL my control names are unique do I HAVE to use the '... of card "MyOtherCard" ' syntax to ref controls on another card or...

Please don't misunderstand me guys, the above is by way of explanation and genuine, thought provoking / learning querying not by way of any excuse offering or pickiness ! Just trying to live, learn and do things correctly from the outset to save me re-working things....... it's just so damn painful when this type of thing bites my backside (especially after a couple of weeks of coding... grrr... I'll shut-up and go mend my code...

Thanks a mil guys - much appreciated.
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1

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

Re: Duplicate control names

Post by Klaus » Tue Sep 03, 2013 1:33 pm

Hi Traxgeek,
Traxgeek wrote:So, not trying to be picky or anything - just tidying up and (saving myself the effort
of having to go and try) when you guys already know :
-1- if I have a control in a group (both uniquely named) do I HAVE to use the '... of group "MyGroup" ' syntax or not ?
No, if the names are really uniqe!
Traxgeek wrote:-2- duplicating a control name on a different card is perfectly acceptable ?
Yes, see 3.
Traxgeek wrote:-3- if ALL my control names are unique do I HAVE to use the '... of card "MyOtherCard" '
syntax to ref controls on another card or...
Yes! For Livecode everything is relative to the current card, so if you are accessing controls of other cards
or even stacks you need to tell Livecode so!


Best

Klaus

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Duplicate control names

Post by MaxV » Tue Sep 03, 2013 2:26 pm

I think that you have to change the point of view: names are like labels for human being, Livecode automatically assign ID. ID are unique per type inside a type. For example a field is not a button, so they can have the same ID.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

Traxgeek
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Wed Jan 09, 2013 10:11 am

Re: Duplicate control names

Post by Traxgeek » Tue Sep 03, 2013 3:44 pm

OK - thanks chaps. Much appreciated.

The golden rule appears to be - DO NOT DUPLICATE A CONTROL NAME (OF THE SAME 'TYPE' OF CONTROL) ON ANY ONE PARTICULAR CARD EVEN IF THE CONTROLS EXIST IN DIFFERENT GROUPS ETC.

I did about 10years with VB and 5 with PCSoft and as long as the controls were isolated from each other / not at the same level then duplicating control names (of the same type or not) was perfectly acceptable. In fact it simply wasn't possible to assign a control a name that (as far as the IDE was concerned) may cause a problem...

I'm learning... and half way though repairing / updating my code to reflect this.

I do, however, have one other question - if the control I wish to reference (ctrl1) is in a group (grp1) is it any faster (at run time) to action : 'set the text of field "ctrl1" to "Some data" ' as opposed to the more specific : 'set the text of field "ctrl1" of group "grp1" to "Some data" ' - No real use I'm sure but just handy to know with longer repeats / loops etc. I guess, reading the above it's probably faster (infinitisimally so - granted) to leave out the ' of group...' and faster to type !!

Cheers and thanks a million again.
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Duplicate control names

Post by jacque » Tue Sep 03, 2013 6:44 pm

MaxV wrote:I think that you have to change the point of view: names are like labels for human being, Livecode automatically assign ID. ID are unique per type inside a type. For example a field is not a button, so they can have the same ID.
Actually, all IDs are unique regardless of the object type. You can't have two controls with the same ID, and there can be problems if you do.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Duplicate control names

Post by jacque » Tue Sep 03, 2013 6:54 pm

Traxgeek wrote: The golden rule appears to be - DO NOT DUPLICATE A CONTROL NAME (OF THE SAME 'TYPE' OF CONTROL) ON ANY ONE PARTICULAR CARD EVEN IF THE CONTROLS EXIST IN DIFFERENT GROUPS ETC.
It's okay to have same-named controls on a card as long as you are aware that you need a more specific reference if you want to do something with one of them. There are cases where it's handy, for example, if you want to loop through all the controls on a card but only act on those that have a particular name. You could use a custom property to do that instead, but names are easy to change and you don't need to remember to set up a property. So I think the golden rule is more like "be aware of duplicates and act accordingly."

One example I use sometimes is that I name all label fields "lbl". When I want to clear the contents of fields, I loop through all the fields on the card and put empty into them unless their name is "lbl". It's fast and easy.
I do, however, have one other question - if the control I wish to reference (ctrl1) is in a group (grp1) is it any faster (at run time) to action : 'set the text of field "ctrl1" to "Some data" ' as opposed to the more specific : 'set the text of field "ctrl1" of group "grp1" to "Some data" ' - No real use I'm sure but just handy to know with longer repeats / loops etc. I guess, reading the above it's probably faster (infinitisimally so - granted) to leave out the ' of group...' and faster to type !!
It's likely the other way around -- the less specific your object reference, the more time it will take the engine to locate it. The engine has to loop through everything to find the first instance of that named control unless you point it to a specific object. The more specific, the better. But in any case, unless you have thousands of controls, the time difference will be insignificant.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Traxgeek
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Wed Jan 09, 2013 10:11 am

Re: Duplicate control names

Post by Traxgeek » Tue Sep 03, 2013 9:38 pm

Thanks Jacque...

This makes sense too (but contradicts previous posts)... this would then be much more in line with my previous experience and explain the use of '...of group "grpName"...' syntax.

The problem here then is I appear to be experiencing some intermittent and strange behaviour with reference to duplicate control names (on different 'layers') and copying them between cards. My posts above explain as clearly as I can at the moment. I can two confirm 2 things though : modifying control names to be unique does indeed sort my problem as does deleting the controls that appear to be incorrectly referenced at run time and then recreating them afresh. I also had to remove (remark out) all script references to the controls and then re-enable them ater recreating the new controls...

Really weird though !

Anyway, thanks for all the help / pointers. life is becoming easier but...
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Duplicate control names

Post by jacque » Tue Sep 03, 2013 11:12 pm

Except for the incorrect info about ID numbers (which need to be unique for every object regardless of type) I don't see a conflict, but maybe I wasn't clear enough when I wrote my original response. Let me know if I'm missing something.

In a nutshell: you can name objects anything you want, including duplicate names. If you have duplicate names, you need to specify which of the duplicates you are referring to by using an object reference that is specific enough for the engine to identify it fully.

LiveCode tries to extend the reference if you don't provide a full one, which means internally it's constructing a full reference (which is why it's probably a bit faster to just provide one in the script.) It always assumes the object is on the current card if you don't specify a card; if the object isn't found there, it throws an error. If the object is on the current card but you provide only a name, LiveCode will assume you mean the first object of that name that it finds, which will be the one that has the lowest layer (regardless of what group it's in.) If you have duplicate-named objects inside duplicate-named groups, you're probably asking for trouble, but LiveCode will still try to resolve the reference by choosing the lowest-level group and the lowest-level object of that name in the group.

So when copying an object that has the same name as another object, you'd need to be specific: copy button "somebutton" of group "somegroup" of card "othercard". You can omit the card reference if the button is on the current card. If you have duplicate-named groups with the same duplicate-named buttons in them, then you can't really use the name to identify controls in the second group if they have names like those in the first one; you'd need to use an ID or some other unique property. If you don't, then LiveCode will find the button in the first group and stop there. Could that be what was happening?

You shouldn't need to delete and recreate controls if their references are specific enough. But as others have said, avoiding duplicate naming is an easy way to manage things, and I always try to do that unless I have a specific reason not to. But sometimes the ability to have duplicate names is a convenience.

If you want to give a little more info about your layout and what objects were causing problems, I bet we could help. A sample stack would be great.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply