Difference between revisions of "Error Messages/40616"
m (→Remedies) |
|||
Line 1: | Line 1: | ||
− | + | [[Category: Error messages]] | |
− | + | == Error message examples == | |
− | + | :<code>''You cannot assign to a slice or subscript of a global variable object.''</code> | |
− | + | == Cause == | |
− | |||
− | |||
− | + | You may not assign a value to a [[slice]] ([[subscript]]ed element) of a global variable. | |
+ | You may assign to a [[slice]] of a [[Local Variables|local variable]]. And in a [[Script]] or Function called from the Script of a [[Button]] or other object, you may assign a new value to a global variable (but not a slice of it.) | ||
+ | |||
+ | This example triggers this error. Here <code>BadDef</code> tries to assign to a slice of the global variable X: | ||
+ | |||
+ | <pre style="background:white; border:white; margin-left: 1em;"> | ||
Index I := 1..9 | Index I := 1..9 | ||
Variable X := Table(I)(7, 8, 3, 2, 5, 4, 9, 1, 5) | Variable X := Table(I)(7, 8, 3, 2, 5, 4, 9, 1, 5) | ||
Variable BadDef := (X[I = 5] := 99) | Variable BadDef := (X[I = 5] := 99) | ||
+ | </pre> | ||
− | = Remedies = | + | == Remedies == |
− | If you want to assign to a slice of a global variable from a [[Scripting|Script]] or a | + | If you want to assign to a [[slice]] of a global variable from a [[Scripting|Script]] or a [[User-Defined Function]] called from a [[Script]], you can do so indirectly. It's usually simplest to write the code in a user-defined Function called from a Script, rather than directly in a Script. In a Function F: |
− | # copy the global array, X, into a local variable, local_x, | + | # copy the global array, <code>X</code>, into a local variable, <code>local_x</code>, |
− | # assign a new value to a slice of local_x, | + | # assign a new value to a slice of <code>local_x</code>, |
− | # assign the value of local_x back to the global, X, | + | # assign the value of <code>local_x</code> back to the global, <code>X</code>, |
− | # finally, call function F from a Button script. | + | # finally, call function <code>F</code> from a [[Button]] script. |
− | For example, suppose you want to assign 999 to the 5th element over index I of global variable X: | + | For example, suppose you want to assign 999 to the 5th element over index <code>I</code> of global variable <code>X</code>: |
+ | <pre style="background:white; border:white; margin-left: 1em;"> | ||
Function Assign_to_X(v) | Function Assign_to_X(v) | ||
Definition: | Definition: | ||
Variable local_x := X; | Variable local_x := X; | ||
− | local_x[I=5] := v; | + | local_x[I =5] := v; |
X := local_x | X := local_x | ||
Button Do_Assign | Button Do_Assign | ||
Script: Assign_to_X(999) | Script: Assign_to_X(999) | ||
+ | </pre> | ||
− | When you press the button, Do_Assign, it calls Assign_to_X(999) and completes the slice assignment. | + | When you press the button, '''Do_Assign''', it calls <code>Assign_to_X(999)</code> and completes the slice assignment. |
− | + | ==See Also== | |
+ | * [[User-Defined Function]] | ||
+ | * [[Local Variables]] | ||
+ | * [[Assignment Operator :=]] | ||
+ | * [[Slice]] | ||
+ | * [[Subscript]] | ||
+ | * [[Script]] | ||
+ | * [[Button]] |
Revision as of 18:00, 31 March 2016
Error message examples
You cannot assign to a slice or subscript of a global variable object.
Cause
You may not assign a value to a slice (subscripted element) of a global variable.
You may assign to a slice of a local variable. And in a Script or Function called from the Script of a Button or other object, you may assign a new value to a global variable (but not a slice of it.)
This example triggers this error. Here BadDef
tries to assign to a slice of the global variable X:
Index I := 1..9 Variable X := Table(I)(7, 8, 3, 2, 5, 4, 9, 1, 5) Variable BadDef := (X[I = 5] := 99)
Remedies
If you want to assign to a slice of a global variable from a Script or a User-Defined Function called from a Script, you can do so indirectly. It's usually simplest to write the code in a user-defined Function called from a Script, rather than directly in a Script. In a Function F:
- copy the global array,
X
, into a local variable,local_x
, - assign a new value to a slice of
local_x
, - assign the value of
local_x
back to the global,X
, - finally, call function
F
from a Button script.
For example, suppose you want to assign 999 to the 5th element over index I
of global variable X
:
Function Assign_to_X(v) Definition: Variable local_x := X; local_x[I =5] := v; X := local_x Button Do_Assign Script: Assign_to_X(999)
When you press the button, Do_Assign, it calls Assign_to_X(999)
and completes the slice assignment.
Enable comment auto-refresher