Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Are Interfaces Pointers/References?

michaelADE
2018-10-11
2020-01-29
  • michaelADE - 2018-10-11

    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

       INTERFACE myInterface EXTENDS __SYSTEM.IQueryInterface
       PROPERTY Name : STRING
    
    FUNCTION_BLOCK myFirstClass IMPLEMENTS myInterface
    VAR
       sName : STRING;
    END_VAR
    METHOD Initialise 
    
    FUNCTION_BLOCK myExtendedClass EXTENDS myFirstClass
    VAR
       sName : STRING;
    END_VAR
    METHOD Initialise 
    
    VAR
       clsMyFirstClass: myFirstClass;
       clsMyExtendedClass: myExtendedClass;
       aMyArray: array [0..3] of myInterface
    END_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'

     

    Related

    Talk.ru: 1
    Talk.ru: 2
    Talk.ru: 3

  • 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.

     
  • michaelADE - 2018-10-11

    Hey Scott

    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] of int;
       iValueOne: int;
       iValueTwo: int;
    end_var
    iValueOne:= 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?

     

    Related

    Talk.ru: 1
    Talk.ru: 2
    Talk.ru: 3

  • michaelADE - 2018-10-11

    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]

     
  • josepmariarams - 2018-10-11

    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

     
  • fatroshi - 2020-01-28

    Josep M. Rams hat geschrieben:
    Hi.
    When you declare a variable as an interface, has to be a pointer to an fb which implements this interface.

    Hi Josep,
    Do you mean something like this?

       interface : POINTER TO MyInterface := ADR(Object_That_implements_MyInterface) ;    // Is this what you mean?
    
     
  • m.prestel - 2020-01-29

    You should never really do this, if you dont really have to and know you are doing.

    Zitat:

       interface : POINTER TO MyInterface := ADR(Object_That_implements_MyInterface) ;    // Is this what you mean?
    

    Back to the question:
    Assigning anything but a fb/interface is copying.

    Best regards,
    Marcel

     

Log in to post a comment.