My intention was to have a generic array of type interface so that i could contain all of the objects that are related to this interface.
I created a function to loop through, i only created one instance of each type of object used its initialisation function wrote it to the interface array. I would then reuse the same instance of the object recalling the initialisation function again to change it and then write it to the array. However at the end even though my array should have been made up of a number of differently configured arrays everywhere i used the same object the array had whatever the last value was for the object, not the value it had at the time of writing it to the array. Is this what should happen? Does this imply that an array of type interface is similar to a pointer/reference where it is just linked to the object that was written to it. If i want the array to be filled with unique objects how would i do this? Do i have to make a base FB for the array instead of the interface?
A very simple example of what im talking about can be seen below
  INTERFACEmyInterfaceEXTENDS__SYSTEM.IQueryInterface  PROPERTYName:STRING
FUNCTION_BLOCKmyFirstClassIMPLEMENTSmyInterfaceVARÂ Â sName:STRING;END_VARMETHODInitialise
FUNCTION_BLOCKmyExtendedClassEXTENDSmyFirstClassVARÂ Â sName:STRING;END_VARMETHODInitialise
VARÂ Â clsMyFirstClass:myFirstClass;Â Â clsMyExtendedClass:myExtendedClass;Â Â aMyArray:array[0..3]ofmyInterfaceEND_VARÂ Â clsMyFirstClass.Initialise('Name 1');Â Â clsMyExtendedClass.Initialise('Name 2');Â Â aMyArray[0]:=clsMyFirstClass;Â Â aMyArray[1]:=clsMyExtendedClass;Â Â clsMyFirstClass.Initialise('Name 3');Â Â clsMyExtendedClass.Initialise('Name 4');Â Â Â Â aMyArray[0]:=clsMyFirstClass;Â Â aMyArray[1]:=clsMyExtendedClass;
aMyArray[0].string = 'Name 3' // I was expecting this to be name 1
aMyArray[1].string = 'Name 4' // I was expecting this to be name 2
aMyArray[2].string = 'Name 3'
aMyArray[3].string = 'Name 4'
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2018-10-11
Originally created by: scott_cunningham
Your issue you found really doesn’t have to do with interfaces. You created two objects, gave them names (name 1 and name 2) and then assigned them to your array. You then renamed your original objects (Name 3 and Name 4) and assigned them to your array again. If you had four objects, your array would display as you expected.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks for your reply so does that mean that arrays are really just references?
If i made the below call
var  aMyArray:array[0..3]ofint;  iValueOne:int;  iValueTwo:int;end_variValueOne:=1;iValueTwo:=2;aMyArray[0]:=iValueOne;aMyArray[1]:=iValueTwo;iValueOne:=3;iValueTwo:=4;aMyArray[2]:=iValueOne;aMyArray[3]:=iValueTwo;
would that not give me
aMyArray[0]= 1
aMyArray[1]= 2
aMyArray[2]= 3
aMyArray[3]= 4
or would it be
aMyArray[0]= 3
aMyArray[1]= 4
aMyArray[2]= 3
aMyArray[3]= 4
If so have you got any recommendations on how to create a number x of objects at run time?
So for any who may be interested this is what I found.
If you make an array of interfaces any objects(function blocks) you write to the array will be REFERENCEs, that is to say that they will be pointers without the need to dereference ^ them. This means that you will need a distinct object for each entry in the array and they will need to remain valid for as long as the array is used.
However if you make an array of Function Blocks then any objects you write to the array will be entered as a new instance of the the object, so you could use the one (or two in my case) objects as creator objects and after writing them to the array you can change them with no change to the array.
if anyone is interested in seeing this work i have attached my testing file to show how i tested this.
Hey Everyone,
My intention was to have a generic array of type interface so that i could contain all of the objects that are related to this interface.
I created a function to loop through, i only created one instance of each type of object used its initialisation function wrote it to the interface array. I would then reuse the same instance of the object recalling the initialisation function again to change it and then write it to the array. However at the end even though my array should have been made up of a number of differently configured arrays everywhere i used the same object the array had whatever the last value was for the object, not the value it had at the time of writing it to the array. Is this what should happen? Does this imply that an array of type interface is similar to a pointer/reference where it is just linked to the object that was written to it. If i want the array to be filled with unique objects how would i do this? Do i have to make a base FB for the array instead of the interface?
A very simple example of what im talking about can be seen below
aMyArray[0].string = 'Name 3' // I was expecting this to be name 1
aMyArray[1].string = 'Name 4' // I was expecting this to be name 2
aMyArray[2].string = 'Name 3'
aMyArray[3].string = 'Name 4'
Related
Talk.ru: 1
Talk.ru: 2
Talk.ru: 3
Originally created by: scott_cunningham
Your issue you found really doesn’t have to do with interfaces. You created two objects, gave them names (name 1 and name 2) and then assigned them to your array. You then renamed your original objects (Name 3 and Name 4) and assigned them to your array again. If you had four objects, your array would display as you expected.
Hey Scott
Thanks for your reply so does that mean that arrays are really just references?
If i made the below call
would that not give me
aMyArray[0]= 1
aMyArray[1]= 2
aMyArray[2]= 3
aMyArray[3]= 4
or would it be
aMyArray[0]= 3
aMyArray[1]= 4
aMyArray[2]= 3
aMyArray[3]= 4
If so have you got any recommendations on how to create a number x of objects at run time?
Related
Talk.ru: 1
Talk.ru: 2
Talk.ru: 3
So for any who may be interested this is what I found.
If you make an array of interfaces any objects(function blocks) you write to the array will be REFERENCEs, that is to say that they will be pointers without the need to dereference ^ them. This means that you will need a distinct object for each entry in the array and they will need to remain valid for as long as the array is used.
However if you make an array of Function Blocks then any objects you write to the array will be entered as a new instance of the the object, so you could use the one (or two in my case) objects as creator objects and after writing them to the array you can change them with no change to the array.
if anyone is interested in seeing this work i have attached my testing file to show how i tested this.
Testing Files.project [239.38 KiB]
Hi.
An instance of an FB is a copy of his variables. The methods are the same for all instances of an fb. An FB is an structure with associated methods.
As an interface has not variables, has no sense talking about instances of interfaces.
When you declare a variable as an interface, has to be a pointer to an fb which implements this interface.
An interface by himself could not be anything because has not variables and has nothing to point to.
Sent from my Moto G (5S) Plus using Tapatalk
Hi Josep,
Do you mean something like this?
You should never really do this, if you dont really have to and know you are doing.
Back to the question:
Assigning anything but a fb/interface is copying.
Best regards,
Marcel