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

Store different data types in same array

fatroshi
2020-01-29
2020-01-29
  • fatroshi - 2020-01-29

    Hi,

    Is it possible to store different data types in an array if they implement the same interface?
    Or in any other way...?

    // Interface
    INTERFACE MyInterface
    FUNCTION_BLOCK A IMPLEMENTS MyInterface
    FUNCTION_BLOCK B IMPLEMENTS MyInterface
    FUNCTION_BLOCK Wrapper
    VAR
    Β  Β objects : ARRAY [0..5] OF ???;Β  // I need to store A and B objects in here, is this possible? 
    END_VAR 
    
     
  • dFx

    dFx - 2020-01-29

    Could this be achieved using a union datatype of A and B ?

     
  • sgronchi - 2020-01-29

    The array would not store objects, just "references" to them. You need to provide storage outside the array (or you go by dynamic memory allocation).

     
  • fatroshi - 2020-01-29

    dFx Thank you for your reply, that could be a solution for the problem but imagine if you had 50 different objects. My goal is to store different data types.

    e.kislov Thank you for the link, It helped a lot. But sadly as SGronchi mentioned the array would only store references to them. I will look into dynamic memory allocation and see if I can manage to resolve this.

    Thanks once again everybody for the help!

     

Log in to post a comment.