I'm trying to pass an array of an extended datatype as a VAR_IN_OUT to a funtionblock:
I have a struct "Object":
TYPE Object :
Position: INT := -1;
and i have an extended object "Box":
TYPE Box EXTENDS Object :
I would like to pass an array of all kinds of object to a functionblock, for example:
objects: ARRAY[*] OF Object;
But when i try to pass an array of boxes, i get a compile error:
boxes: ARRAY[0..9] OF BOX;
moveObj(objects := boxes);
Last line gives me a "cannot convert type 'ARRAY[0..9] OF BOX' to type 'ARRAY[*] OF Object'. The error is not about the size of the array (0..9 vs *), but about the box and object.
How do i solve this?
"cannot convert type 'ARRAY[0..9] OF BOX' to type 'ARRAY[*] OF Object"
I think the error is that you try to transfer an ARRAY of BOX into an ARRAY of Object.
But a BOX contains 3 elements (Position, Width, Color) and an Object contains only 2 elements (Position, Width) ! System don't know what to do with "Color" !!
You have to use a same type of ARRAY, or to transform Object type into Box Type.
For example :
aObject: ARRAY[*] OF Object; (source table)
aBox:ARRAY[*] OF Box; (destination table)
iIndice:DINT; (Work index)
diBegin:DINT; (Start of table)
diEnd:DINT; (End of table)
(Calculation of the dimensions of the table)
(Copy source table to destination table)
FOR iIndice:= diBegin TO diEnd DO
aInboxes: ARRAY[0..9] OF Box; (destination table)
aInObjects: ARRAY[0..9] OF Object; (source table)
Transform(aObject:=aInObjects , aBox:=aInboxes );
isn't the whole point of extending a struct that an object of the extended struct is also considered as an object of the base struct?
In my example, I want 1 fucntionblock to move objects. I would like this functionblock to be reusable to move any kind of object, either base or extended. I don't need the color of the box to move it.
Instead of using your solution, where an array of boxes is converted into an array of objects, I can just as well write a second functionblock moveBoxes, defeating the whole point of extending my object...
Note that if I create a functionblock MoveObject that moves 1 object, this idea holds:
boxes: ARRAY[0..9] OF BOX;
//moveObjects(objects := boxes);
moveObject(object := boxes);
It is only when i start using arrays that it does not work anymore
ok, so an array puts the different objects directly behind each other in memory, so it is not possible to change the size (add or remove properties) of the individual object. I figured this out after trying the same thing in c++.
BUT... even with an array of pointers to an object, it does not work... This should work, no?
There are some caveats on OOP usage in automation (PLC/DCS) systems. One of those is instantiation of objects in runtime, which is possible, but should be handled with extra care.
On my git (not yest hosted on forge... sry)
(just basics, no fancy stuff)
My previous remark is still right, and you saw it with your example in C++
In any case, even with a pointer, if you try to move an array into another (whatever the type of data), the destination array must have the same structure as the source array.
When you use only the first source table level, it works well, because system take the two first element ( Position and Width) and forget the third, because destination table consist of 2 elements.
But when you use the entire table, system don't know how to fill destination table, because structures are different.
I ended up using a array of interfaces. Not intirely happy because i still need 2 array's (one for the objects, an other one for the interfaces), but it works
@aliazz: I really liked your git repository on OOP concepts. Thanks!
Log in to post a comment.