Difference between revisions of "Error Messages/40616"

m
 
(One intermediate revision by one other user 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 may not assign a value to a slice (subscripted element) of a global variable.  
+
== Cause ==
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:
+
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]]. 
  
Index I := 1..9
+
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:
Variable X := Table(I)(7, 8, 3, 2, 5, 4, 9, 1, 5)
 
Variable BadDef := (X[I = 5] := 99)
 
  
= Remedies =
+
<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: X[I = 2] := 30
 +
</pre>
  
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:
+
== Remedies  ==
# 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:
+
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>,
 +
 
 +
For example:
 
   
 
   
  Function Assign_to_X(v)
+
<pre style="background:white; border:white; margin-left: 1em;">
    Definition:  
+
Index I := 1..2
        Variable local_x := X;
+
Variable X := Table(I)(10, 20)
        local_x[I=5] := v;
+
Button <code>Change_X</code>   
        X := local_x
+
  OnClick:  
 
+
      VAR local_X := X;  
  Button Do_Assign
+
      local_X[I = 2] := 30;  
    Script:  Assign_to_X(999)
+
      X := local_X
 +
</pre>
  
When you press the button, Do_Assign, it calls Assign_to_X(999) and completes the slice assignment.
+
This does what you need.
  
<br><comments />
+
==See Also==
 +
* [[User-Defined Function]]
 +
* [[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.