Passing
Variables
WML allows the defining of variables which can then be used
between cards within a deck, or between decks. This is not possible in HTML. The
setting of variables and then passing them between cards can increase the
flexibility of the functions done within decks or between decks. The variables
are set with the <setvar/> command and the required attributes, name and value. Usually the <setvar/>
command is used between a <go>...</go>,
<prev>...</prev>, or a <refresh>...</refresh> command.
The name attribute of the <setvar/>
command defines the identifier or name of the variable that WML code can refer
to, using $variable or the $(variable) form. The value
attribute sets the value of the variable.
Another common way to define and set the value of variables is
with the <input/> command. The <input/> command allows for the user
to input text and when the name attribute is used, that
identifier is assigned the value of the inputted data, Also, in the
<input/> command, the value attribute can be used to
assign an initial value to the variable named in the same command.
Example 4.3 shows
the passing of a variable between cards in the same deck. When card 1 is entered
going forward, the variable State is set to the value “Initial”. When the button
“Change” is pushed, the variable State is set to “Changed”. And, when the Return
button is pushed, the variable State is set to “Returned”. Figure 4.3 shows the screens for Example 4.3. ( Note that in Example 4.3 both forms of the variable “State” are used
:$State and $(State). Technically the parentheses are only needed if necessary
to separate the variable name from surrounding text.)
Probably the most common passing of variables is done between a
WML deck and a WMLScript program. WMLScript is discussed in Chapter 5 and 6, but the passing of
variables to WMLScript functions from WML decks is done by using the
<setvar/> command or the <input/> command. In the WMLScript
function, the same variable names are used and thus the passing of variables is
very easy. As will be discussed in the chapters on WMLScript, the variables (or
new variables defined in the WMLScript function) can then be easily passed back
to the WML deck. Example 4.4
shows the code for passing a variable named “number” to a WMLScript function
named “factorial” in a WMLScript program named “calculate.wmls”. The variable is
assigned in an <input> command, and the value is determined by the user
input. Note that the variable “result” is used by the WMLScript function to
return the value of the factorial calculation.