Initial values for variables
When you declare a PowerScript variable, you can accept the
default initial value or specify an initial value in the declaration.
Default values for variables
If you do not initialize a variable when you declare it, PowerBuilder
sets the variable to the default value for its datatype as shown
in the following table.
|
For this variable datatype |
PowerBuilder sets this |
|---|---|
|
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 (byte, integer, long, longlong, decimal, real, double, UnsignedInteger, and UnsignedLong) |
0 |
|
String |
Empty string (“”) |
|
Time |
00:00:00 (midnight) |
Specifying a literal as a initial value
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”.
Do not use a function’s return value
You should not initialize a variable by assigning it the return
value of a global user defined function, because it might not compile
correctly, or because it could lead to confusion about the value
assigned. For example, do not use:
|
1 |
integer i = f_return_one() |
Although you can use global system functions or expressions
to initialize variables with compile time values in a variable declaration
statement, for runtime value assignments, you must also declare
variables and assign their values in separate statements.
This example declares li_count as
an integer whose value is 5:
|
1 |
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:
|
1 |
integer li_a=5, li_b=10 |
This example initializes ls_method with
the string “UPS”:
|
1 |
string ls_method="UPS" |
This example initializes ls_headers to
three words separated by tabs:
|
1 |
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:
|
1 |
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, 2004:
|
1 |
date ld_StartDate = 2004-02-01 |
Specifying an expression as an initial value
You can initialize a variable with the value of an existing
variable or expression, such as:
|
1 |
integer i = 100 |
|
1 |
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 at runtime.
If the expression’s value changes
Because the expression’s value is set to the variable
when the script is compiled (not at runtime) 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:
|
1 |
date d_date = Today( ) |
In contrast, these statements result in d_date being
set to the date the application is run:
|
1 |
date d_date |
|
1 |
d_date = Today( ) |
How shared variables are initialized
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.
How instance variables are initialized
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