When you declare a PowerScript variable, you can accept the default initial value or specify an initial value in the declaration.
If you do not initialize a variable when you declare it, PocketBuilder sets the variable to the default value for its datatype as shown in the following table.
For this variable datatype |
PocketBuilder sets this default value |
---|---|
Blob |
A blob of 0 length; an empty blob |
Char (or character) |
ASCII value 0 |
Boolean |
false |
Date |
1900-01-01 (January 1, 1900) |
DateTime |
1900-01-01 00:00:00 |
Numeric (integer, long, longlong, decimal, real, double, UnsignedInteger, and UnsignedLong) |
0 |
String |
Empty string ("") |
Time |
00:00:00 (midnight) |
To initialize a variable when you declare it, place an equal sign (=) and a literal appropriate for that variable datatype after the variable. For information about literals for specific datatypes, see “Standard datatypes”.
This example declares li_count as an integer whose value is 5:
integer li_count=5
This example declares li_a and li_b as integers and initializes li_a to 5 and li_b to 10:
integer li_a=5, li_b=10
This example initializes ls_method with the string "UPS":
string ls_method="UPS"
This example initializes ls_headers to three words separated by tabs:
string ls_headers = "Name~tAddress~tCity"
This example initializes li_a to 1 and li_c to 100, leaving li_b set to its default value of zero:
integer li_a=1, li_b, li_c=100
This example declares ld_StartDate as a date and initializes it with the date February 1, 1993:
date ld_StartDate = 1993-02-01
You can initialize a variable with the value of an existing variable or expression, such as:
integer i = 100
integer j = i
When you do this, the second variable is initialized with the value of the expression when the script is compiled. The initialization is not reevaluated during execution.
If the expression’s value changes Because the expression’s value is set to the variable when the script is compiled (not during execution) make sure the expression is not one whose value is based on current conditions. If you want to specify an expression whose value will be different when the application is executed, do not initialize the variable in the declaration. For such values, declare the variable and assign the value in separate statements.
In this declaration, the value of d_date is the date the script is compiled:
date d_date = Today( )
In contrast, these statements result in d_date being set to the date the application is run:
date d_date
d_date = Today( )
When you use a shared variable in a script, the variable is initialized when the first instance of the object is opened. When the object is closed, the shared variable continues to exist until you exit the application. If you open the object again without exiting the application, the shared variable will have the value it had when you closed the object.
For example, if you set the shared variable Count to 20 in the script for a window, then close the window, and then reopen the window without exiting the application, Count will be equal to 20.
When using multiple instances of windows If you have multiple instances of the window in the example above, Count will be equal to 20 in each instance. Since shared variables are shared among all instances of the window, changing Count in any instance of the window changes it for all instances.
When you define an instance variable for a window, menu, or application object, the instance variable is initialized when the object is opened. Its initial value is the default value for its datatype or the value specified in the variable declarations.
When you close the object, the instance variable ceases to exist. If you open the object again, the instance variable is initialized again.
When to use multiple instances of windows When you build a script for one of multiple instances of a window, instance variables can have a different value in each instance of the window. For example, to set a flag based on the contents of the instance of a window, you would use an instance variable.
When to use shared variables instead Use a shared variable instead of an instance variable if you need a variable that:
Keeps the same value over multiple instances of an object
Continues to exist after the object is closed