Typically, to access the dynstruct effectively, you need to get the information about the fields organized in way to facilitate easy systematic access. The MX_DYNSTRUCT DYNOP_INFO call returns the layout information for a previously defined dynstruct. You can reference it by its name, or via a dynstruct variable which has been bound to the dynstruct definition. The layout is returned to you in an array of structures containing all the relevant attributes for each field. The array is of type ST_FLDDEF, defined in ashinc:dynstruct.def; see the MX_DYNSTRUCT topic for its definition. Note that since the subroutine is not able to auto-extend the array, either you must make two calls—the first to get the number of fields, the second to get the actual array after re-dimensioning it to the necessary size—or use the fndynst.bsi wrapper function which takes care of those details. Here we get the layout of the previously defined ST_TEST dynstruct, from the first example, and from that we’ll create a simple array of field names to be used later for accessing the members by name
map1 fields,i,4
map1 i,i,4
dimx flddefs(10),ST_FLDDEF ! array of field definitions
dimx fnames$(0), T_DYN_NAME, auto_extend ! array of field names
fields = Fn'Dynst'Get'Def'By'Name("ST_TEST",flddefs())
? "fields: ";fields; ifelse$(fields >= 0, " [ok]"," [Error]")
if fields > 0 then
for i = 1 to fields
fnames$(i) = flddefs(i).name
next i
endif
It may seem pointless to copy the field names from the flddefs() array of ST_FLDDEF structures into the simple array fnames$(), but we need a simple array, i.e. an array of strings rather than structures for syntactic reasons when actually referencing the dynstruct members. See Indirect Deferred Syntax Limitations.
Also note that while in most cases, all we need in order to access the structure members are the field names, if any of the structure members are arrays, we’ll need to get that information from the flddefs() array and use it to form alternate syntax for accessing the array members. The syntax for referencing an array variable is necessarily different from that for scalar variables.