Abstract:Create variable names and reference its value at run-time.
What is it?
Dynamic variable references is the ability to generate a variable name by including variable references, strings, and function calls within a variable reference.
Why would I want to use it?
Although at first this might seem to be a very obscure enhancement, dynamic variable reference fills a critical gap in Net.Data's ability to handle lists that are generated at run-time when the number of elements in the list cannot be determined in advance. Examples of such lists are HTML form elements that are generated based on records returned from an SQL query.
Prior to this enhancement, there was no technique available in Net.Data or in client-side scripting to dynamically access a variable. You needed to know the name of the variable in advance. If your Net.Data macro generated form elements and assigned names during the generation, you could not retrieve the values unless you hard-coded the element name. For example, assume that you run an SQL SELECT statement to create a Net.Data table, with any number of elements. You can create an HTML form with input fields like this:
Because you created INPUT fields, you would probably want to access the values that the user entered when the form is submitted to your macro for processing.
Prior to dynamic variables, there was no technique you could use to dynamically reference the values. You needed to use variable names such as $(I1), $(I2)...$(In), which were assigned in advance. The only solution was to limit the number of rows to a known quantity that you programmed for in advance.
How do I use it?
A dynamic variable references can consist of variable references, strings, and function calls within a variable reference. You cannot assign a value to the dynamically generated names, you can only reference the variable. For example:
Listing 2. Test dynamic reference
%define my = "my"
%define u = "lower"
%define myLOWERvar = "hey"
%HTML(test) {
$($(my)@dtw_ruppercase(u)var)
%}
In the above macro, the variable reference returns the value of hey.
If we wanted to retrieve the variables coming in due to the form in Listing 1 being submitted, we can loop to retrieve the values in a variable length list as follows:
Listing 3. Show form data
.
.
.
@dtw_assign(rowIndex, "1")
%while (rowIndex <= rowCount) {
The value entered for row $(rowIndex) is: $(I$(rowIndex))
@dtw_add(rowIndex, "1", rowIndex)
%}
.
.
.
In the example above, Net.Data first generates the variable name using the I$(rowIndex) reference. For example, the first variable name would be I1. Net.Data then uses that value and resolves to the value of the variable using the $(I1) name.
More information on dynamic variable references can be found in the Net.Data Reference.
Availability
V3R2, V3R7 and subsequent releases. Ensure you have the latest Net.Data PTF.