Assignment
Description
Assigns values to variables or object properties or object
references to object variables.
Syntax
|
1 |
variablename = expression |
|
Argument |
Description |
|---|---|
|
variablename |
The name of the variable or object property to which |
|
expression |
An expression whose datatype is compatible with |
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 2 |
String1 = "Part is out of stock" 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 2 |
int Arr[] 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 ave 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
Example 1
These statements each assign a value to the variable ld_date:
|
1 2 3 4 |
date ld_date ld_date = Today( ) ld_date = 2006-01-01 ld_date = Date("January 1, 2006") |
Example 2
These statements assign the parent of the current control to a
window variable:
|
1 2 |
window lw_current_window lw_current_window = Parent |
Example 3
This statement makes a CheckBox invisible:
|
1 |
cbk_on.Visible = FALSE |
Example 4
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) |
Example 5
These statements concatenate two strings and assign the value to the
string Text1:
|
1 2 |
string Text1 Text1 = sle_emp.Text+".DAT" |
Example 6
These assignments use operator shortcuts:
|
1 2 3 4 |
int i = 4i ++ // i is now 5. i -- // i is 4 again. i += 10 // i is now 14. 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 2 3 |
int i, j i = 12 j = i ++ // INVALID |
The following is valid, because ++ is used by itself in the
assignment:
|
1 2 3 4 |
int i, j i = 12 i ++ j = i |