What is the best way to iterate through components or properties?

Hi all,

Currently we have a script in a toggle button event itemStateChanged that iterates over 19 components, checking the current, previous high and previous low values, then sets a new previous high or low. So that's 57 values involved.

listOfZones = ['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19']
if event.source.selected:
	for item in listOfZones:
		currentValue=event.source.parent.parent.getComponent('CZ '+item+' Current Value').value
		if event.source.parent.parent.getComponent('CZ '+item+' Current Value').previousHigh == 1e-2:
			event.source.parent.parent.getComponent('CZ '+item+' Current Value').previousLow = currentValue / 5
			event.source.parent.parent.getComponent('CZ '+item+' Current Value').previousHigh = event.source.parent.parent.getComponent('CZ '+item+' Current Value').previousLow * 10
		if currentValue < event.source.parent.parent.getComponent('CZ '+item+' Current Value').previousLow * 1.18 or currentValue > event.source.parent.parent.getComponent('CZ '+item+' Current Value').previousHigh*.9:
			event.source.parent.parent.getComponent('CZ '+item+' Current Value').previousLow = currentValue / 5
			event.source.parent.parent.getComponent('CZ '+item+' Current Value').previousHigh = event.source.previousLow * 10

I was reading some best practice tips on this thread (Do's and Don'ts when developing First Project with Ignition? - #2 by danielps1818) and am wondering if it would be reasonable to try and stuff this into a project script. It will only run on a radio button selection, so once or twice while the user is interacting with the screen.
While considering this, to try and move away from the getComponent() stuff, I thought I would use custom props on the root container. That would mean, currently, 57 custom props and I would need a way to iterate over those based on the name as shown in the listOfZones above. Something like event.source.parent._{zoneName}currentValue equating to event.source.parent._01currentValue or something similar. Then retreiving and or modifying those values.

Do you have any recommendations on this? Am I better off leaving it as is?
Thanks

Let my understand this. You want to iterate throw 19 components to set new high and low values? but only when a button is press (a toggle button).

The first solution I'm thinking (without testing) would be creating a custom propertie that is a dict.
Then you could link each component with it's ID key and set it's high and low value. Then make that link bidirectional so whenever you press the button you can simply write in that dictionary.

Seems like Message Handler is perfect fit for this task:

1 Like

Use intermediate variables to hold containers and components. And don't initialized variables if you aren't going to use them. Like so:

if event.source.selected:
	container = event.source.parent.parent
	listOfZones = ['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19']
	for item in listOfZones:
		component = container.getComponent('CZ '+item+' Current Value')
		currentValue=component.value
		if component.previousHigh == 1e-2:
			component.previousLow = currentValue / 5
			component.previousHigh = component.previousLow * 10
		if currentValue < component.previousLow * 1.18 or currentValue > component.previousHigh*.9:
			component.previousLow = currentValue / 5
			component.previousHigh = event.source.previousLow * 10

The script will run faster and be easier to maintain.

This is Vision, not Perspective.

I like this a lot better. And, for my mind's sake, it probably does not make any sense to use a library script here, as it seems it would make this much more complicated. Many values to pass back and forth for each component.

I would use a library script, passing the event object.