The Any datatype
General information
PowerBuilder also supports the Any datatype,
which can hold any kind of value, including standard datatypes,
objects, structures, and arrays. A variable whose type is Any is
a chameleon datatype—it takes the datatype of the value assigned
to it.
Do not use Any in EAServer componentdefinition
The Any datatype is specific to PowerScript
and is not supported in the IDL of an EAServer component.
CORBA has a datatype called Any that can assume any
legal IDL type at runtime, but it is not semantically equivalent
to the PowerBuilder Any type. You must exclude
the PowerBuilder Any datatype from the component
interface definition, but you can use it within the component.
Declarations and assignments
You declare Any variables just as you
do any other variable. You can also declare an array of Any variables,
where each element of the array can have a different datatype.
You assign data to Any variables with
standard assignment statements. You can assign an array to a simple Any variable.
After you assign a value to an Any variable,
you can test the variable with the ClassName function
and find out the actual datatype:
|
1 |
any la_spreadsheetdata |
|
1 |
la_spreadsheetdata = ole_1.Object.cells(1,1).value |
|
1 |
CHOOSE CASE ClassName(la_spreadsheetdata) |
|
1 |
CASE "integer" |
|
1 |
... |
|
1 |
CASE "string" |
|
1 |
... |
|
1 |
END CHOOSE |
These rules apply to Any assignments:
-
You can assign anything into
an Any variable. -
You must know the content of an Any variable
to make assignments from the Any variable to
a compatible datatype.
Restrictions
If the value of a simple Any variable
is an array, you cannot access the elements of the array until you
assign the value to an array variable of the appropriate datatype.
This restriction does not apply to the opposite case of an array
of Any variables—you can access each Any variable
in the array.
If the value of an Any variable is a
structure, you cannot use dot notation to access the elements of
the structure until you assign the value to a structure of the appropriate
datatype.
After a value has been assigned to an Any variable,
it cannot be converted back to a generic Any variable
without a datatype. Even if you set it to NULL,
it retains the datatype of the assigned value until you assign another
value.
Operations and expressions
You can perform operations on Any variables
as long as the datatype of the data in the Any variable
is appropriate to the operator. If the datatype is not appropriate
to the operator, an execution error occurs.
For example, if instance variables ia_1 and ia_2 contain
numeric data, this statement is valid:
|
1 |
any la_3 |
|
1 |
la_3 = ia_1 - ia_2 |
If ia_1 and ia_2 contain
strings, you can use the concatenation operator:
|
1 |
any la_3 |
|
1 |
la_3 = ia_1 + ia_2 |
However, if ia_1 contained a
number and ia_2 contained a string,
you would get an execution error.
Datatype conversion functions
PowerScript datatype conversion functions accept Any variables
as arguments. When you call the function, the Any variable
must contain data that can be converted to the specified type.
For example, if ia_any contains
a string, you can assign it to a string variable:
|
1 |
ls_string = ia_any |
If ia_any contains a number
that you want to convert to a string, you can
call the String function:
|
1 |
ls_string = String(ia_any) |
Other functions
If a function’s prototype does not allow Any as
a datatype for an argument, you cannot use an Any variable
without a conversion function, even if it contains a value of the
correct datatype. When you compile the script, you get compiler
errors such as Unknown function or Function.
not found
For example, the argument for the Len function
refers to a string column in a DataWindow,
but the expression itself has a type of Any:
|
1 |
IF Len(dw_notes.Object.Notes[1]) > 0 THEN // Invalid |
This works because the string value of
the Any expression is explicitly converted
to a string:
|
1 |
IF Len(String(dw_notes.Object.Notes[1])) > 0 THEN |
Expressions whose datatype is Any
Expressions that access data whose type is unknown when the
script is compiled have a datatype of Any.
These expressions include expressions or functions that access data
in an OLE object or a DataWindow object:
|
1 |
myoleobject.application.cells(1,1).value |
|
1 |
dw_1.Object.Data[1,1] |
|
1 |
dw_1.Object.Data.empid[99] |
The objects these expressions point to can change so that
the type of data being accessed also changes.
Expressions that refer to DataWindow data can return arrays
and structures and arrays of structures as Any variables.
For best performance, assign the DataWindow expression to the appropriate
array or structure without using an intermediate Any variable.
Overusing the Any datatype
Do not use Any variables as a substitute
for selecting the correct datatype in your scripts. There are two
reasons for this:
-
At execution
time, using Any variables is slowPowerBuilder must do much more processing to determine datatypes
before it can make an assignment or perform an operation involving Any variables.
In particular, an operation performed many times in a loop will
suffer greatly if you use Any variables instead
of variables of the appropriate type. -
At compile time, using Any variables
removes a layer of error checking from your programmingThe PowerBuilder compiler makes sure datatypes are correct
before code gets executed. With Any variables, some
of the errors that can be caught by the compiler are not found until the
code is run.