How to use struct elements via index?

stade
2020-06-22
2020-06-23
  • stade - 2020-06-22

    Hi, I have a struct with elements in different datatype, for example:

    TYPE DataExchange :
    STRUCT
    Data1 : INT;
    Data2 : WORD;
    Data3 : DWORD;
    END_STRUCT
    END_TYPE

    VAR
    DataRead : DataExchange;
    END_VAR

    Is there any way to use the elements in the struct via index?
    It means using
    DataRead[1] := 0 instead of DataRead.Data1 := 0,
    DataRead[2] := 0 instead of DateRead.Data2 := 0, etc.

    Thanks.

     

    Related

    Talk.ru: 1
    Talk.ru: 2


    Last edit: stade 2020-06-22
  • aliazzz

    aliazzz - 2020-06-22

    Structs are NOT Types!

    Apparently you want a Struct of your own type which is achieved like this:

    Exchange : ARRAY [0..31] OF DataExchange;
    

    Usage

    Exchange[0].Data1 := a;
    Exchange[0].Data2 := b;
    Exchange[0].Data3 := c;
    ..
    ..
    Exchange[31].Data1 := x;
    Exchange[31].Data2 := y;
    Exchange[31].Data3 := z;
    

    Offcourse 0..31 and a,b,c,x,y,z are illustrative.

    Regards,

    Aliazzz

     

    Last edit: aliazzz 2020-06-22
    • stade - 2020-06-22

      Hi aliazzz.

      Actually I have hundreds of elements with different datatype in the struct, which means there are Data1 .. Data1000(or more).
      The names of element are illustrative, actually they are irregular.
      I have only 1 DataExchange named DataRead, and I want to use index as parameter to call the element I need.
      For example, when I want to call Data50, I may use index 50 to call this element, instead of using DataRead.Data50.
      But I have no idea how to implement it.

      Sorry for my bad statement and thanks for your answer.

       

      Last edit: stade 2020-06-22
  • aliazzz

    aliazzz - 2020-06-22

    You are (from what I understand) trying to describe an array of pointers to your elements instances but i have difficulties in understanding your writing, so I could misinterpret it.

    (* pointers are *dangerous* when not initialised correct *)
    Collection : ARRAY [0..31] OF POINTER TO DataExchange;
    
    (* I am not sure on this ^ statement! it could also be a pointer to DWORD ?=> check ).
    Always initialise the array of pointers in the beginning of your code and continously update them. This might be tedious but then again it helps you from crashes and whatnot! *)
    
    
    (* INIT Pseudo code, must be run at start of every cycle(!) in order to survive
    "online change" *)
    Collection[0] := ADR(DataExchange00)
    Collection[1] := ADR(DataExchange01)
    Collection[2] := ADR(DataExchange03)
    ..
    ..
    Collection[N] := ADR(DataExchangeN)
    Collection[N+1] := ADR(DataExchangeN+1)
    
    
    (* Writing Usage Example Pseudo *)
    Collection[2]^.Data1 := .. ;
    Collection[2]^.Data2 := .. ;
    Collection[2]^.Data3 := .. ;
    
    
    (* reading Usage Pseudo*)
    .. := Collection[1]^.Data1;
    .. := Collection[1]^.Data2;
    .. := Collection[1]^.Data3;
    

    Good luck!

     

    Related

    Talk.ru: 1
    Talk.ru: 2


    Last edit: aliazzz 2020-06-22
    • stade - 2020-06-23

      It looks like every DataExchangeN in your code means different DataExchange.
      I have only 1 DataExchange and hundreds of variables with different datatype in this DataExchange.

      So what I want is likely

      Collection[0] point to DataRead.Data1,
      Collection[1] point to DataRead.Data2,
      ..
      Collection[N] point to DataRead.DataN+1.

      I will try if pointer can do this, thanks.

       

      Related

      Talk.ru: 1

  • aliazzz

    aliazzz - 2020-06-23

    If you have cracked your problem, would you mind posting/sharing your solution here?
    Thanks

     

    Last edit: aliazzz 2020-06-23

Log in to post a comment.