Using a variable to refer to custom properties

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
sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Using a variable to refer to custom properties

Post by sritcp » Thu Mar 05, 2015 4:49 pm

I have a repeat loop that strings together the names of custom properties

Code: Select all

repeat for each item tMenuChoice in sMenuList
      put the ("c" & tMenuChoice & "NumOfParas") of this card into tNum
........
which gives the compilation error: "Expression: bad factor" in the parenthesized region.
The parenthesis parses correctly if tested independently.

This raises a question: Do property names always have to be literal? That is, is the statement
put the variableContainingPropertyName of the yourObject
not possible?

Thanks,
Sri.

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

Re: Using a variable to refer to custom properties

Post by Klaus » Thu Mar 05, 2015 4:53 pm

Hi sri,

use IT:

Code: Select all

repeat for each item tMenuChoice in sMenuList
      get "c" & tMenuChoice & "NumOfParas"
      put the IT of this card into tNum
........
or a variable with its name already concatenated!

Code: Select all

repeat for each item tMenuChoice in sMenuList
      put "c" & tMenuChoice & "NumOfParas" into tProp
      put the tProp of this card into tNum
........
Best

Klaus

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: Using a variable to refer to custom properties

Post by magice » Thu Mar 05, 2015 4:56 pm

Try it in a "do" statement.

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

Re: Using a variable to refer to custom properties

Post by FourthWorld » Thu Mar 05, 2015 5:07 pm

The suggestions given thus far are good ones, and here's one more I find especially convenient for cases in which I need to work with property names that can't be known in advance: use array syntax.

This will put a value into a custom property within a custom property set named "MyPrefs", and the second handler retrieves it:

Code: Select all

on SetValue pKey, pVal
   set the MyPrefs[pKey] of stack "SomePrefs" to pVal
   save stack "SomePrefs"
end SetValue

on GetValue pKey
    return the MyPrefs[pKey] of stack "SomePrefs"
end GetValue
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Re: Using a variable to refer to custom properties

Post by sritcp » Thu Mar 05, 2015 5:42 pm

Thanks everyone, for the suggestions!

Klaus,
I didn't think of trying

Code: Select all

put "c" & tMenuChoice & "NumOfParas" into tProp
      put the tProp of this card into tNum
because I thought that was exactly what parenthesis did (evaluate what is inside it and put it in a temporary variable).
I guess logic doesn't always work!

Regards,
Sri.

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

Re: Using a variable to refer to custom properties

Post by Klaus » Thu Mar 05, 2015 6:02 pm

Hi sri,
sritcp wrote:Klaus,
I didn't think of trying

Code: Select all

put "c" & tMenuChoice & "NumOfParas" into tProp
      put the tProp of this card into tNum
because I thought that was exactly what parenthesis did (evaluate what is inside it and put it in a temporary variable).
I guess logic doesn't always work!
yep, the parenthesis sound logical, but the engine is extremely picky in this case :D


Best

Klaus

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Re: Using a variable to refer to custom properties

Post by phaworth » Thu Mar 05, 2015 8:44 pm

I entered bug# 13086 at the QCC q wgile back. Seemed to get a favorable review by Mark Waddingham but it hasn't been fixed yet.
Pete

Post Reply