Using structures
After you define the structure, you can:
- Reference an instance of the structure in scripts
and functions - Pass the structure to functions
- Display and paste information about structures by
using the Browser
Referencing structures
When you define a structure, you are defining a new data type.
You can use this new data type in scripts and user-defined functions
as long as the structure definition is stored in a library in the
application’s library search path.
To use a structure in a script or user-defined
function:
- Declare a variable of the structure type.
- Reference the variable in the structure.
Referencing global structures
The variables in a structure are similar to the properties
of a PowerBuilder object. To reference a global structure’s
variable, use dot notation:
|
1 |
<i>structure.variable</i> |
Example Assume that emp_data is a global structure with the
variables emp_id, emp_dept, emp_fname,
emp_lname, and emp_salary.
To use this structure definition, declare a variable of type
emp_data and use dot notation to reference the structure’s
variables, as shown in the following script:
|
1 |
emp_data emp1, emp2 // Declare 2 variables<br /> // of type emp_data.<br /><br />emp1.emp_id = 100 // Assign values to the<br />emp1.emp_dept = 200 // structure variables.<br />emp1.emp_fname = "John"<br />emp1.emp_lname = "Paul-Jones"<br />emp1.emp_salary = 19908.23<br /><br />// Retrieve the value of a structure variable.<br />emp2.emp_salary = emp1.emp_salary * 1.05<br /><br />// Use a structure variable in a<br />// PowerScript function.<br />MessageBox ("New Salary", &<br /> String(emp2.emp_salary,"$#,##0.00")) |
Referencing object-level structures
You reference object-level structures in scripts for the object
itself exactly as you do global structures: declare a variable of
the structure type, then use dot notation:
|
1 |
<i>structure.variable</i> |
Example Assume that the structure cust_data is defined for
the window w_history and you are writing a script for a
CommandButton in the window. To use the structure definition in
the script, you write:
|
1 |
cust_data cust1<br />cust1.name = "Joe" |
Allowing access to object-level structures outside
the object
You can also choose to make object-level structures accessible
outside the object.
To reference object-level structures outside the
object:
-
In the object that defines the structure,
declare an instance variable of the structure type. -
In any script or user-defined function in the
application, reference a variable in the structure using the following
syntax:1<i>object.instance_variable.variable</i>
Example Assume you have defined a structure named cust_data
for the window w_history and you want to be able to use
it anywhere in your application. Define an instance variable of
type cust_data for w_history:
|
1 |
cust_data winstruct |
In other scripts, reference the window structure through the
instance variable, such as:
|
1 |
w_history.winstruct.name |
Copying structures
To copy the values of a structure to another structure
of the same type:
-
Assign the structure to be copied to the
other structure using this syntax:1<i>struct1 = struct2</i>PowerBuilder copies all the variable values from struct2 to
struct1.
Example These statements copy the values in emp2 to emp1:
|
1 |
emp_data emp1, emp2<br />...<br />emp1 = emp2 |
Using structures with functions
You can pass structures as arguments in user-defined functions.
Simply name the structure as the data type when defining the argument. Similarly,
user-defined functions can return structures. Name the structure
as the return type for the function.
You can also define external functions that take structures
as arguments.
Example Assume the following:
- Revise is an external
function that expects a structure as its argument. - emp_data is a declared variable of a structure
data type.
You can call the function as follows:
|
1 |
Revise(emp_data) |
Declare the function first The external function must be declared before you can reference
it in a script.
For more about passing arguments to external
functions, see Application Techniques
.
Displaying and
pasting structure information
You can display the names and variables of defined structures
in the Browser. You can also paste these entries into a script.
To display information about a global structure
in the Browser:
- Select the Structure tab and select a structure.
-
Double-click the properties folder in the right
pane.The properties folder expands to show the structure variables
as properties of the structure.
To display information about an object-level structure
in the Browser:
-
Select the tab for the type of object for
which the structure is defined. - Select the object that contains the structure.
-
Double-click the structure folder in the right
pane.The structure folder expands to display the structure variables
using dot notation.
To paste the information into a script:
-
Scroll to the structure variable you want
to paste. -
Select Copy from the variable’s popup
menu. -
Insert the cursor in the script where you want
to paste the variable and select Paste from the popup menu.The variable name displays at the insertion point in the script.
This part describes how to create windows for your application.
It covers the properties of windows, the controls you can place
in windows, how to use inheritance to save time and effort, and
how to define menus. It also introduces user objects and user events.