Hi, I have a struct with elements in different datatype, for example:
TYPE DataExchange :
Data1 : INT;
Data2 : WORD;
Data3 : DWORD;
DataRead : DataExchange;
Is there any way to use the elements in the struct via index?
It means using
DataRead := 0 instead of DataRead.Data1 := 0,
DataRead := 0 instead of DateRead.Data2 := 0, etc.
Structs are NOT Types!
Apparently you want a Struct of your own type which is achieved like this:
Exchange : ARRAY [0..31] OF DataExchange;
Exchange.Data1 := a;
Exchange.Data2 := b;
Exchange.Data3 := c;
Exchange.Data1 := x;
Exchange.Data2 := y;
Exchange.Data3 := z;
Offcourse 0..31 and a,b,c,x,y,z are illustrative.
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.
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 := ADR(DataExchange00)
Collection := ADR(DataExchange01)
Collection := ADR(DataExchange03)
Collection[N] := ADR(DataExchangeN)
Collection[N+1] := ADR(DataExchangeN+1)
(* Writing Usage Example Pseudo *)
Collection^.Data1 := .. ;
Collection^.Data2 := .. ;
Collection^.Data3 := .. ;
(* reading Usage Pseudo*)
.. := Collection^.Data1;
.. := Collection^.Data2;
.. := Collection^.Data3;
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 point to DataRead.Data1,
Collection point to DataRead.Data2,
Collection[N] point to DataRead.DataN+1.
I will try if pointer can do this, thanks.
If you have cracked your problem, would you mind posting/sharing your solution here?
Log in to post a comment.