Custom Property that is an array

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
tjm167us
Posts: 50
Joined: Sat Dec 03, 2011 8:27 pm

Custom Property that is an array

Post by tjm167us » Sun Mar 13, 2016 10:45 pm

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

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

Re: Custom Property that is an array

Post by [-hh] » Sun Mar 13, 2016 11:46 pm

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.]
shiftLock happens

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

Re: Custom Property that is an array

Post by dunbarx » Mon Mar 14, 2016 5:20 am

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

tjm167us
Posts: 50
Joined: Sat Dec 03, 2011 8:27 pm

Re: Custom Property that is an array

Post by tjm167us » Tue Mar 15, 2016 6:07 pm

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.

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

Re: Custom Property that is an array

Post by jacque » Wed Mar 16, 2016 5:01 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply