Assignment
Description
Assigns values to variables or object properties or object
references to object variables.
Syntax
1 |
<span>variablename </span> = <span>expression</span> |
Argument |
Description |
---|---|
variablename |
The name of the variable or object property |
expression |
An expression whose datatype is compatible |
Usage
Use assignment statements to assign values to variables. To
assign a value to a variable anywhere in a script, use the equal
sign (=). For example:
1 |
String1 = "Part is out of stock" |
1 |
TaxRate = .05 |
No multiple assignments
Since the equal sign is also a logical operator, you cannot
assign more than one variable in a single statement. For example,
the following statement does not assign the value 0 to A and B:
1 |
A=B=0 // This will not assign 0 to A and B. |
This statement first evaluates B=0
to true or FALSE and
then tries to assign this boolean value to A. When A is not a boolean
variable, this line produces an error when compiled.
Assigning array values
You can assign multiple array values with one statement, such
as:
1 |
int Arr[] |
1 |
Arr = {1, 2, 3, 4} |
You can also copy array contents. For example, this statement copies
the contents of Arr2 into array Arr1:
1 |
Arr1 = Arr2 |
Operator shortcuts
The PowerScript shortcuts for assigning values to variables
in the following table have slight performance advantages over their equivalents.
Assignment |
Example |
Equivalent to |
---|---|---|
++ |
i ++ |
i = i + 1 |
— |
i — |
i = i – 1 |
+= |
i += 3 |
i = i + 3 |
-= |
i -= 3 |
i = i -3 |
*= |
i *= 3 |
i = i * 3 |
/= |
i /= 3 |
i = i / 3 |
^= |
i ^=3 |
i = i ^ 3 |
Unless you have prohibited the use of dashes in variable names,
you must leave a space before --
and -=.
If
you do not, PowerScript reads the minus sign as part of a variable
name. For more information, see “Identifier names “.
Examples
These statements each assign a value to the variable ld_date:
1 |
date ld_date |
1 |
ld_date = Today( ) |
1 |
ld_date = 2006-01-01 |
1 |
ld_date = Date("January 1, 2006") |
These statements assign the parent of the current control
to a window variable:
1 |
window lw_current_window |
1 |
lw_current_window = Parent |
This statement makes a CheckBox invisible:
1 |
cbk_on.Visible = FALSE |
This statement is not an assignment—it tests the
value of the string in the SingleLineEdit sle_emp:
1 |
IF sle_emp.Text = "N" THEN Open(win_1) |
These statements concatenate two strings and assign the value
to the string Text1:
1 |
string Text1 |
1 |
Text1 = sle_emp.Text+".DAT" |
These assignments use operator shortcuts:
1 |
int i = 4 |
1 |
i ++    // i is now 5. |
1 |
i --    // i is 4 again. |
1 |
i += 10  // i is now 14. |
1 |
i /= 2   // i is now 7. |
These shortcuts can be used only in pure assignment
statements. They cannot be used with other operators in a statement.
For example, the following is invalid:
1 |
int i, j |
1 |
i = 12 |
1 |
j = i ++   // INVALID |
The following is valid, because ++ is
used by itself in the assignment:
1 |
int i, j |
1 |
i = 12 |
1 |
i ++ |
1 |
j = i |