Using the DWObject variable in PowerBuilder

A PowerBuilder DWObject object is an object that exists within a DataWindow object. Each column, computed field, text control, or drawing control is a DWObject.

A DWObject reference allows you to refer directly to controls within a DataWindow.

You can use a DWObject variable to simplify DataWindow property and data expressions. A DWObject variable takes the place of several elements of the control's dot notation.

The following syntaxes and examples show how using a DWObject variable affects property and data expressions.

Property expressions

The simple syntax for a property expression is:

dwcontrol.Object.dwcontrolname.property

You can use a DWObject variable to refer to dwcontrolname.

If the code declares a DWObject variable and assigns the control within the DataWindow to the variable, using syntax like this:

DWObject dwobjectvar
dwobjectvar = dwcontrol.Object.dwcontrolname

the syntax of the expression itself becomes:

dwobjectvar.property

For example, if the DataWindow had a column named empname, a text control named t_emplabel, and a computed field named cf_average, you could make the following assignments:

DWObject dwo_column, dwo_text, dwo_compute
dwo_column = dw_1.Object.empname
dwo_text = dw_1.Object.t_emplabel
dwo_compute = dw_1.Object.cf_average

Data expressions

You can use a DWObject variable to refer to a column in a data expression. For example, this syntax gets data for a single row and column:

dwcontrol.Object.columnname {.buffer } {.datasource } [ rownum ]

If the code declares a DWObject variable and assigns the control within the DataWindow to the variable, using syntax like this:

DWObject dwobjectvar
dwobjectvar = dwcontrol.Object.columnname

The syntax of the expression itself becomes:

dwobjectvar. {.buffer } {.datasource } [ rownum ]

DWObject variables in PowerBuilder

In PowerBuilder, you can get better performance by using a DWObject variable to resolve the object reference in a DataWindow property or data expression. Evaluating the reference once and reusing the resolved reference is more efficient than fully specifying the object reference again.

This technique yields the most benefit if your application uses compiled code or if you are using a DataWindow expression in a loop.

For example, this code is not optimized for best performance, because the fully specified data expression within the loop must be resolved during each pass:

integer li_data
FOR li_cnt = 1 to 100
      li_data = dw_1.Object.emp_salary[li_cnt]
      .. // Code to process data value
NEXT

This code has been optimized. The reference to the control within the DataWindow (emp_salary) is resolved once before the loop begins. The reference stored in the DWObject variable is reused repeatedly in the loop:

integer li_data
DWObject dwo_empsalary

dwo_empsalary = dw_1.Object.emp_salary

FOR li_cnt = 1 to 100
      li_data = dwo_empsalary.Primary[li_cnt]
      .. // Code to process data value
NEXT

PowerBuilder DWObject versus data

In a data expression for a column that refers to one item, the brackets for the row index identify the expression as a data expression (for information, see Syntax for one or all data items in a named column). However, if you assign the column control to a DWObject variable, the brackets incorrectly signify an array of objects. Therefore you must include a buffer name or data source to specify that you want data:

dw_1.Object.emp_salary[1] //Single data item
DWObject dwo_empsalary
dwo_empsalary = dw_1.Object.emp_salary
dwo_empsalary[1] // Incorrect: array of DWObject
dwo_empsalary.Primary[1] // Single data item

DWObject arguments for DataWindow events in PowerBuilder

In PowerBuilder, several DataWindow events pass a DWObject argument called dwo to the event script. The value is a resolved reference to a control within the DataWindow having something to do with the user's action that triggered the event. Often it is the column the user is changing or the control the user clicked.

What type of DWObject?

You can use DataWindow properties to find out more about the control stored in dwo. The first step is to find out the control's type so that subsequent statements will use properties that are appropriate for the control type. If an expression uses a property that does not correspond to the control's type, it will trigger the Error event. This statement in an event script gets the type:

ls_type = dwo.Type

The possible values that can be assigned to ls_type are:

bitmap (for Picture)
button
column
compute (for Computed Field)
graph
groupbox
line
ole
ellipse (for Oval)
rectangle
roundrectangle
report
tableblob
text
datawindow (when the user doesn't click a specific control)

You can write a CHOOSE CASE statement for the expected types.

After you have determined the type, you can get more details about the specific control.

Examples

If the control is a column, you can get the column name with this statement:

ls_name = dwo.Name

If the control is a column, you can get data from the whole column or from specific rows. You must specify the buffer from which you want to retrieve data. In this statement, row is another argument passed to the event so the value in ls_data is the data in the row and column the user clicked. In this example, if the column value is not a string, an error occurs (check ColType property to get the column datatype):

ls_data = dwo.Primary[row]

This statement assigns a new value to the row and column the user clicked. The assignment does not trigger the ItemChanged event and bypasses validation. If the column is not numeric, an error occurs:

dwo.Primary[row] = 41

This statement gets all the data in the column the user clicked. The data is stored as an array in the Any variable. An Any variable can hold all datatypes, so no error occurs:

Any la_data
la_data = dwo

This statement gets data in the column from selected rows. The data is stored as an array in the Any variable:

Any la_data
la_data = dwo.Selected