using a variable for a custom property 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

Post Reply
Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

using a variable for a custom property name

Post by Da_Elf » Mon Oct 13, 2014 3:25 pm

This might seem like a weird one but id like to do something like this but it doesnt work...

Code: Select all

put 1 into advan
repeat for 5 times
  put "Item"&advan into customName
  set customName of me to "X"
  add 1 to advan
end repeat
The result im looking for is 5 custom properties listed as Item1, Item2, Item3, Item4, Item5

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

Re: using a variable for a custom property name

Post by Klaus » Mon Oct 13, 2014 3:29 pm

Hi Da_Elf,

THE, the magic word for creating/accessing c_props is THE, set/get THE custompropname 8)

This works:

Code: Select all

...
put 1 into advan
repeat for 5 times
  put "Item" & advan into customName
  set THE customName of me to "X"
  add 1 to advan
end repeat
...
Best

Klaus

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: using a variable for a custom property name

Post by Thierry » Mon Oct 13, 2014 3:31 pm

Da_Elf wrote: The result im looking for is 5 custom properties listed as Item1, Item2, Item3, Item4, Item5
What about this?

Code: Select all

   repeat with J=1 to 5
      get "myCP_" & J
      set the IT of me to J
   end repeat
Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

Re: using a variable for a custom property name

Post by Da_Elf » Mon Oct 13, 2014 4:00 pm

actually i went one better on it and created a new set of custom keys so i didnt need to use "THE" (i dont use "the" when setting properties but i use it when getting them)

set uStuff[variablename] of me to "X"
this gives me to power to delete the group of custom properties from "me" when im finished with them without disturbing the other normal custom properties

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

Re: using a variable for a custom property name

Post by Klaus » Mon Oct 13, 2014 4:27 pm

Da_Elf wrote:... (i dont use "the" when setting properties but i use it when getting them)...
Yep, that's what Hermann wrote, but that does not make it officially correct PER SE! 8)

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: using a variable for a custom property name

Post by [-hh] » Mon Oct 13, 2014 11:03 pm

"The" is optional before properties.
That's a law of the engine, not by me. But Klaus, just to please you, I added line 2 in the first script below (uncomment it after using it once).
Seeing this horrible bug I admit, that it is better not to use "empty", "it", "custom" or other popular LC names as custom property names. And to use "the", because it's better to understand. (But not "teh", as I often do; Simon, your influence.)

Code: Select all

 -- May behave different with diff LC versions !!
on mouseUp
  set customName of me to "I'm your customName."
  -- put "Hermann works hard to prepare his next error." into customName
  # Bug (?) The line above replaces property customName of me
  answer the customName of me # <-- "the" is superfluous but my style
end mouseUp
Another crazy example that works:

Code: Select all

on mouseUp
  set 7.0 of me to "I dreamt to be 8.0"
end mouseUp
Look in the object inspector to see, that the property is set.
Because we are in the beginner-subforum I don't say how to get this property, because "get the 7.0 of me" doesn't work.
Anyway, nice trick of LC to "hide" something that we are getting here.

[Edit: Changed example above to avoid the special case of it. I saw too late, that LC behaves different with different versions and dependent on whether properties are set a first time or not, say: Watch what is in RAM while you use 4 different stacks named "Untitled 1". Sorry.]
Last edited by [-hh] on Tue Oct 14, 2014 12:19 am, edited 2 times in total.
shiftLock happens

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

Re: using a variable for a custom property name

Post by dunbarx » Mon Oct 13, 2014 11:16 pm

Hermann.

It works, but it also does not work;

Code: Select all

on mouseUp
     set the 7.0 of me to "I dreamt to be 8.0"
  answer the 7.0 of me
end mouseUp
Try it. What happens is surprising. Can a line both execute, giving an answer, and also fail to execute? Set aside for a moment that the answer is wrong. I think Schrodinger's cat is hidden in the code.

Craig

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: using a variable for a custom property name

Post by [-hh] » Mon Oct 13, 2014 11:39 pm

Hi Craig.

The engine complains when you try to get the property. Setting depends on LC version.
LC 6.6.3 sets the property only without "the", LC 7 always (because it's dreaming).

Obviously the engine uses different ways to handle the property names when getting and setting.
This is for my advantage, because I often write erraneously "the item 1 of strg" and "the word 3 of strg" when Im testing several approaches. And such "the" are accepted.

Here is moreover the special case of a number that has to be converted to a string (what always implies some evaluation?).
Perhaps one of the engine contributors can explain this, I have no oversight on source.
Craig wrote: ... Schrodinger's cat is hidden in the code ...
Which state would you define as "life" ?

Hermann
shiftLock happens

Post Reply