Accessing Members in the Dynstruct

This example continues from the previous one in which we retrieved the layout of the dynstruct ST_TEST into a flddefs() array, and then created a simple array, fnames$(), of member names. Here we’ll bind the defined dynstruct to a dynstruct variable, make assignments to members via the Simple Indirect syntax, and then retrieve members using the Indirect Deferred syntax with the help of the fnames$() array.

map1 status,i,4              ! status variable

map1 dstest, dynstruct       ! dynstruct variable

map1 fields,i,4              ! # fields (see previous example)

map1 i,i,2                   ! loop variable

dimx flddefs(10),ST_FLDDEF   ! array of field info (see prev example)

dimx fnames$(0), T_MAX_DYNAME, auto_extend ! array of field names (see prev)

 

<define ST_TEST, retrieve flddefs(), create fnames$() per above examples>

 

status = Fn'Dynst'Bind("ST_TEST", dstest)   ! bind ST_TEST Dynstruct to dstest

if status = 0 then          ! success

    ! samples assignments using simple indirect syntax

    dstest.sku = "SKU12345"

    dstest.qty = 2

    dstest.price.retail = 1.99

    dstest.notes(1) = "steal this product"

    dstest.notes(2) = "and live in infamy"

 

    ! sample retrieval/display using indirect deferred syntax

    for i = 1 to fields

        ? dstest.@fnames$(i)    ! warning: see notes below

    next i

endif

 

Although the indirect deferred system is very convenient and indeed essential if the goal is to write totally generic code without any hard-coded references to specific dynamic structure members, there are a couple of complications which need to be considered when iterating through the fields by name; see the subtopics noted below.