You are here: Tips and Tricks > Using Variables

Using Variables

Variables are only intended for use in odd situations. An example is where the length of a string is given sometimes as a byte and sometimes as a word. Do not create variables just because you want to reference field values while processing other fields; in most cases you can just refer directly to the field name for this purpose.

Be careful to avoid any possibility of referencing a variable before it is defined, especially in a case where there are multiple paths through your decoder each of which is supposed to store a particular variable whose value is then used at the end. One technique that may help in this regard is to initialize variables at the start of the executable section of the decoder. This also serves as an excellent opportunity to document them, as in:

/*

** The variable string_len is set to the length of the

** varialbe string if one exists. If there is no such

** string then its value will be left at minus one as

** set here.

*/

FIELD init_var_string_len (Fixed 0) STORE string_len RETRIEVE (StoreInteger ‐1) (Decimal) SUPPRESS_DETAIL

Note that you can use similar means to perform arithmetic operations on variables. Consider, for example:

FIELD arithmetic_1 (Fixed 0) STORE var1 RETRIEVE (StoreField var1) ALSO (MultiplyField var2) ALSO (SubtractInteger 1) (Decimal) SUPPRESS_DETAIL

This multiplies variable var1 by variable var2, subtracts one from that, and then stores the result back into var1. Sure, it would be easier if one could write, say:

var1 = var1 * var2 ‐ 1

But it may only be one decoder in fifty that needs to do arithmetic on variables in the first place.

You can even display the value of a variable in the decode pane, as with the statement:

FIELD display_var1 (Fixed 0) RETRIEVE (StoreField var1) (Decimal) "Variable var1"