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.

NoteDo not use Any in EAServer component definition 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:

any la_spreadsheetdata

la_spreadsheetdata = ole_1.Object.cells(1,1).value

CHOOSE CASE ClassName(la_spreadsheetdata)

	CASE "integer"

		...

	CASE "string"

		...

END CHOOSE

These rules apply to Any assignments:

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:

any la_3

la_3 = ia_1 - ia_2

If ia_1 and ia_2 contain strings, you can use the concatenation operator:

any la_3

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:

ls_string = ia_any

If ia_any contains a number that you want to convert to a string, you can call the String function:

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:

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:

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:

myoleobject.application.cells(1,1).value

dw_1.Object.Data[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: