Page 1 of 1

Custom Property that is an array

Posted: Sun Mar 13, 2016 10:45 pm
by tjm167us
Hello everyone,
It's been a while since I have used LiveCode so I'm a bit rusty. I have made a custom property in my stack that is an array called activePorts. The appropriate element of this property is updated when a particular checkbox is checked in my user interface. Please refer to my stacks initialization code below:

Code: Select all

on openStack
   //Initialize stacks custom properties
   set the activePorts["A"] of me to false
   set the activePorts["B"] of me to false
   set the activePorts["C"] of me to false
   set the activePorts["D"] of me to false
   set the activePorts["E"] of me to false
   set the activePorts["F"] of me to false
end openStack
When a button is pressed, I would like to see which "ports" are active by retrieving the activePorts custom property and iterate through the array. So, in my button code, I do this:

Code: Select all

on mouseUp
   put the activePorts of stack "Port Initialization" into thePorts
//other stuff here
end mouseUp
But, thePorts is left empty after this code executes. However, if I ask for a single element of the array, everything works:

Code: Select all

put the activePorts["A"] of stack "Port Initialization" into thePorts
//thePorts = true when the Port A checkbox is checked, false otherwise
How do I access the entire array instead of just one element in my button handler?
Thanks,
Tom

Re: Custom Property that is an array

Posted: Sun Mar 13, 2016 11:46 pm
by [-hh]
Hi Tom,

in your openstack (in your stack's script) you define a customPropertySet "activePorts" that collects the customProperties activePorts["A"] etc.

You can use that as an array as follows.

Code: Select all

put the customProperties["activePorts"] of this stack into thePorts
And then, for example:

Code: Select all

--[1] as usual:
put thePorts["A"] &comma& thePorts["C"] into toCheck
--[2] in LC 7 and greater where length(delimiter)>1 is now allowed:
combine thePorts by comma and " is "
put thePorts
Hermann

p.s. You could think about using "this stack" instead of "me" in the openstack handler, because this handler can then moved, for example, also to card's script (doing this, I fooled me once using still "me" ...)

[Edit. Sorry, my second example, when corrected, was too complicated, removed it.]

Re: Custom Property that is an array

Posted: Mon Mar 14, 2016 5:20 am
by dunbarx
Hi.

Do you see what Hermann was saying? There are subtleties in the use of arrays, and making them visible. Make a button and a field. Try this in the button script:

Code: Select all

on mouseUp
   put "false" into activePorts["A"] 
   put "false" into activePorts["B"] 
   put "false" into activePorts["C"] 
   put "false" into activePorts["D"] 
   answer activePorts
   combine activePorts with return
     put activePorts into fld 1
end mouseUp
You can see the contents of the variable in the debugger, but not in the dialog. You have to deconstruct an array with the "combine" command in order to "see" it in what I call "the clear".

Craig Newman

Re: Custom Property that is an array

Posted: Tue Mar 15, 2016 6:07 pm
by tjm167us
Hermann and Craig,
Thank you both for your (prompt and thoughtful) replies - I read about the customPropertySet stuff, but didn't realize I inadvertently made one!

From a design perspective, am I using custom properties as they were intended? I could have also used a global variable to do the same thing, but it seemed like a sloppier way of doing things since this method doesn't lend itself to the idea of encapsulation. Does anyone have other ideas for keeping track of the state of multiple UI elements? I also thought of grouping all of the check boxes together, and then iterating through them all to check their "hilite" property when needed instead of constantly updating the activePorts custom property of my main stack.

Re: Custom Property that is an array

Posted: Wed Mar 16, 2016 5:01 pm
by jacque
If I were doing this, I'd probably read the check boxes directly, for two reasons. First, there's no reason to duplicate storage when the checkboxes already have the information. Second, as you said, you don't need to do any scripting to maintain a variable.