﻿# Dynamic Structures

A Dynamic Structure (aka "Dynstruct") is like a regular structure—i.e., one defined with [DEFSTRUCT](defstruct.md)—except that the definition occurs at runtime rather than compile time. This allows general-purpose compiled routines to adapt at run-time to a wide variety of data structures or record layouts, including ones not known about in advance. Typical applications include:

•	Generic file or audit trail viewers.

•	Report generators.

•	Handlers for SQL query results or CSV imports.

•	Security hardening of existing code (by modifying record structures at runtime to remove unauthorized fields without breaking code referencing the authorized fields).

•	Creating XTREE array layouts on the fly.

•	Decoupling field-level logic from the physical layout of the containing record, i.e. adopting a more SQL-like approach to data.

Dynamic Structures in A-Shell are implemented with the [MX\_DYNSTRUCT](mx_dynstruct.md) subroutine, which see for additional and related information.

**Dynstruct's Four Steps**

There are four steps to using dynamic structures:

•	**[Declare](declare_a_dynstruct.md)**[ a Dynstruct variable](declare_a_dynstruct.md) to contain the structure

•	**[Define](define_the_dynstruct.md)**[ the Dynstruct layout](define_the_dynstruct.md)

•	**[Bind](bindthedynstruct.md)**[ the Dynstruct to a Variable](bindthedynstruct.md)

•	**[Reference](referenceusagedynamic.md)**[ the Dynstruct structure](referenceusagedynamic.md) and members as you would traditional structures / members.

**Typographical Note**

The term "Dynstruct" is used here loosely as a shorthand for "dynamic structure" or for the A-Shell dynamic structure implementation in general. There is also a data type of the same name. When referring to it specifically, we’ll use all upper case (DYNSTRUCT), even though data type names in ASB source code are not case sensitive. Code examples may use DYNSTRUCT and dynstruct interchangeably. 

**History**

2022 February, A-Shell 6.5.1711: Add [Structure Definition Embedding](structuredefinitionembedding.md) (see following topic)

**Subtopics**

- [Declare a Dynstruct Variable](declare_a_dynstruct.md)

- [Define the Dynstruct Layout](define_the_dynstruct.md)

- [Bind the Dynstruct to a Variable](bindthedynstruct.md)

- [Reference / Usage](referenceusagedynamic.md)

- [Dynstruct Limitations](dynstructlimitations.md)

- [Dynstruct Error Handling](dynstructerrorhandling.md)

- [Dynamic Structure Examples](dynamicstructureexamples.md)