Difference between revisions of "Error Messages/40616"

m
 
Line 7: Line 7:
 
== Cause  ==
 
== Cause  ==
  
You may not assign a value to a [[slice]] ([[subscript]]ed element) of a global variable.
+
Analytica won't let you assign a value to a [[slice]] ([[subscript]]ed element) of a global variable, even in an [[OnClick]] or [[OnChange]] attribute, or Function called from one of those, where you would normally be able to [[Assignment Operator :=|assign]] to a global variable. It will let you assign to a [[slice]] of a [[Local Variables|local 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.)
+
Here the [[OnClick]] attribute of Button <code>Change_X</code>tries to assign to a slice of the global variable <code>X</code>, and so triggers this error message:
 
 
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;">
 
<pre style="background:white; border:white; margin-left: 1em;">
  Index I := 1..9
+
  Index I := 1..2
  Variable X := Table(I)(7, 8, 3, 2, 5, 4, 9, 1, 5)
+
  Variable X := Table(I)(10, 20)
  Variable BadDef := (X[I = 5] := 99)
+
  Button <code>Change_X</code>   
 +
        OnClick: X[I = 2] := 30
 
</pre>
 
</pre>
  
 
== Remedies  ==
 
== Remedies  ==
  
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:
+
Analytica ''does'' let you assign to a slice of a local variable (in any Definition), and it ''does'' let you assign to a global variable (not a slice) in an [[OnClick]] or [[OnChange]] attribute, or Function called from one of those attributes. So you can work around this problem.  
# copy the global array, <code>X</code>, into a local variable, <code>local_x</code>,
+
# Copy the global array, <code>X</code>, into a local variable, <code>local_x</code>,
# assign a new value to a slice of <code>local_x</code>,  
+
# Assign a new value to a slice of <code>local_x</code>,  
# assign the value of <code>local_x</code> back to the global, <code>X</code>,
+
# Assign the value of <code>local_x</code> back to the global, <code>X</code>,
# finally, call function <code>F</code> from a [[Button]] script.
 
  
For example, suppose you want to assign 999 to the 5th element over index <code>I</code> of global variable <code>X</code>:
+
For example:
 
   
 
   
 
<pre style="background:white; border:white; margin-left: 1em;">
 
<pre style="background:white; border:white; margin-left: 1em;">
  Function Assign_to_X(v)
+
Index I := 1..2
    Definition:  
+
Variable X := Table(I)(10, 20)
        Variable local_x := X;
+
Button <code>Change_X</code>   
        local_x[I =5] := v;
+
  OnClick:  
        X := local_x
+
      VAR local_X := X;  
 
+
      local_X[I = 2] := 30;  
  Button Do_Assign
+
      X := local_X
    Script:  Assign_to_X(999)
 
 
</pre>
 
</pre>
  
When you press the button, '''Do_Assign''', it calls <code>Assign_to_X(999)</code> and completes the slice assignment.
+
This does what you need.
  
 
==See Also==
 
==See Also==

Latest revision as of 17:59, 8 July 2016


Error message examples

You cannot assign to a slice or subscript of a global variable object.

Cause

Analytica won't let you assign a value to a slice (subscripted element) of a global variable, even in an OnClick or OnChange attribute, or Function called from one of those, where you would normally be able to assign to a global variable. It will let you assign to a slice of a local variable.

Here the OnClick attribute of Button Change_Xtries to assign to a slice of the global variable X, and so triggers this error message:

 Index I := 1..2
 Variable X := Table(I)(10, 20)
 Button <code>Change_X</code>    
        OnClick: X[I = 2] := 30

Remedies

Analytica does let you assign to a slice of a local variable (in any Definition), and it does let you assign to a global variable (not a slice) in an OnClick or OnChange attribute, or Function called from one of those attributes. So you can work around this problem.

  1. Copy the global array, X, into a local variable, local_x,
  2. Assign a new value to a slice of local_x,
  3. Assign the value of local_x back to the global, X,

For example:

 Index I := 1..2
 Variable X := Table(I)(10, 20)
 Button <code>Change_X</code>    
   OnClick: 
      VAR local_X := X; 
      local_X[I = 2] := 30; 
      X := local_X

This does what you need.

See Also

Comments


You are not allowed to post comments.