Difference between revisions of "Error Messages/40616"

(→‎Remedies: show workaround)
m
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
= Error Message Examples  =
+
[[Category: Error messages]]
  
  You cannot assign to a slice or subscript of a global variable object.
+
== Error message examples ==
  
= Cause  =
+
:<code>''You cannot assign to a slice or subscript of a global variable object.''</code>
  
You are trying to assign a value to a slice (subscripted element) of a global variable. This is not permitted, since it would contravene Analytica's general principle of ''no side-effects''. You may assign to a slice of a local variable.
+
== Cause ==
  
This example triggers this error. Here Object2 is assigning to a slice of the global variable object, Object1.
+
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]].  
  
Definition of Index1:<br>
+
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:
<code>1..9</code>
 
  
Definition of Object1:<br>
+
<pre style="background:white; border:white; margin-left: 1em;">
<code>Table(Index1)(7, 8, 3, 2, 5, 4, 9, 1, 5)</code>
+
Index I := 1..2
 +
Variable X := Table(I)(10, 20)
 +
Button <code>Change_X</code>   
 +
        OnClick: X[I = 2] := 30
 +
</pre>
  
Definition of Object2:<br>
+
== Remedies  ==
<code>Object[Index1 = 5] := 6 </code>
 
  
= 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.
 +
# 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 the value of <code>local_x</code> back to the global, <code>X</code>,
  
You may assign to a global variable in a Button script or in a Function called from a Button script [[Scripting|Scripts and Buttons]], directly or indirectly. The rationale is that this does not violate the No-side effects principle because clicking a button means the user is causing the effect directly.
+
For example:
 +
 +
<pre style="background:white; border:white; margin-left: 1em;">
 +
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
 +
</pre>
  
You can often work around this issue by copying the global array into a local, assigning to a slice of the array, and then assigning the local back to the global within a function called from a Button script. For example, suppose you want to assign 999 to the 10th element over I of global variable X.  If you define a variable
+
This does what you need.
  Variable TrytoAssignX := X[I=10] := 999
 
it will give the error above.
 
So instead define
 
  Function AssigntoX(v)
 
  Definition: Variable local_X:= X;
 
        local_X[i=10] := v;
 
        X := local_X
 
  Button Assign_to_X
 
  Script:  Assign_to_X(999)
 
  
When you press the button, it will call Assign_to_X(999) and make the assignment.
+
==See Also==
 
+
* [[User-Defined Function]]
<br><comments />
+
* [[Local Variables]]
 +
* [[Assignment Operator :=]]
 +
* [[Slice]]
 +
* [[Subscript]]
 +
* [[Script]]
 +
* [[Button]]

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.