You can modify the number of elements of the array on runtime by modifying the Dimension1 property of the UDA. The Dimension1 property is not user writeable, so a script is needed to modify the property. If you resize an array up to more elements, the new added elements will have the default value of the corresponding data type (e.g. 0 for numbers, false for Booleans, and "" for strings); on the other hand, if you resize the array down to a smaller dimension, the values of the "deleted" elements will be lost.
Find a small example after the break.
For our example, we will use a test User Defined object with two UDAs: the array UDA and a helper UDA that will allow us to set the array dimension on runtime.
1. Derive a template from the $UserDefined object and name it $Test.
2. Add a UDA and configure it as follows:
- Name: ArrayTest
- Data type: Integer (any data type will do)
- Category: User writeable (any category will do)
- This is an array: checked
- Number of elements: 5
- Set the initial values to each index for illustration purposes.
3. Add a UDA and configure it as follows:
- Name: SetArraySize
- Data type: Integer
- Category: User writeable
- This is an array: leave unchecked
- Set the initial value to: 5 (the initial value should match the initial size of the array)
- Name: ResizeArray
- Execution type: Execute
- Expression: Me.SetArraySize
- Trigger type: DataChange
- Body of the script:
Me.ArrayTest.Dimension1 = Me.SetArraySize;
- Lock the script.
5. Create an instance of the $Test template and deploy it.
6. Open Object Viewer and add the ArrayTest[-1] and SetArraySize attributes to the watch list.
7. Modify SetArraySize to 10 to grow ArrayTest to 10 elements. Notice the new 5 elements added with a default value of 0 (for an Integre data type).
8. Modify SetArraySize to 3 to shrink ArratTest to 3 elements. Notice that the last 7 elements are removed from the array.
9. Modify SetArraySize to 5 to grow ArrayTest back to 5 elements. Notice that the new 2 elements added have the default value is 0 (for an Integer data type).
.
Hi.
ReplyDeleteI'm David Tarazona, the Wonderware Tech Support Engineer at Colsein Ltda., the local Wonderware distributor for Colombia.
I've a lot of experience in advanced C++ programming (like programming with memory efficiency with pointers, templates, etc...) and found the article interesting...
This would work for an application I'm helping to develop with a local Systems Integrator...
Thanks a lot.