Arrays of Dynstructs

As noted above in Declare a DYNSTRUCT Variable, the dynstruct type currently only supports mapped scalar variables; no arrays, mapped or dimx. However, as also noted, you can work around this limitation by copying freely between dynstructs and arrays of traditional structures or X variables. In this example, we retrieve instances of ST_TEST structures—defined in the example Define a Dynstruct from a String—from a function, copying each to an array of type X that was previously dimensioned to match the size of ST_TEST. Then we reverse the operation, copying elements out of the array back into the scalar dynstruct variable to print the members.

map1 dsdef$,s,0             ! string to hold definition source

map1 fields,i,2             ! # fields (signed to allow error codes)

map1 stsize,b,4             ! size of the structure

map1 errmsg$,s,100          ! error messages

map1 ds, DYNSTRUCT          ! var to contain the dynstruct ST_TEST

 

! assume dsdef$ string already built to define ST_TEST (see first example)

! now define it and get the size

 

fields = Fn'Dynst'Define(dsdef$,stsize,"ST_TEST",errmsg$)

if fields > 0 then

    dimx testarray(0), X, stsize, auto_extend   ! array to match dynstruct size

 

    do while <some condition>

        ds = Fn’Get’Structure’Data$("ST_TEST")  ! get data into the ds from somewhere

        i += 1

        testarray(i) = ds             ! copy it into our unformatted array

    loop

 

    ! now reverse process, copying from the X array to our ds var

    do while i > 0

        ds = testarray(i)
     ? ds.sku
     ? ds.price.retail
     ? ds.notes(1)

        ? ds.@fname

        ! etc

    Loop

Endif