Using inheritance to build a window
When you build a window that inherits its definition (style,
events, functions, structures, variables, controls, and scripts)
from an existing window, you save coding time. All you have to do
is modify the inherited definition to meet the requirements of the
current situation.
Example
Assume your application has a window w_employee that
has:
- A title (Employee
Data) - Text that says
1Select a file:
- A dropdown listbox with a list of available employee
files - An Open button with a script that opens the selected
file in a multiline textbox - An Exit button with a script that asks the user
to confirm closing the window and then closes the window
The window looks like this:
Now assume you need to build another window that performs
similar processing. The only difference is that the dropdown listbox
displays customer files instead of employee files and there is a
Delete button so the user can delete files.
Your choices
To build this window, you have three choices:
- Build a new window
from scratch as described in “Building a new window” - Modify the existing window (w_employee),
then save it under another name - Use inheritance to build a window that inherits
the definition from the existing window (in other words, build a
descendent window)
If you build a new window from scratch, the amount of work
is the same as building the first window. If you use either of the
two other methods, all you have to do is change the title and the
files that display in the dropdown listbox and add a Delete button.
Both methods eliminate work.
Advantages of using inheritance
Using inheritance has a number of advantages:
- When you change the ancestor window,
the changes are reflected in all descendants of the window. You
do not have to manually make changes in the descendants as you would
in a copy. This saves you coding time and makes the application
easier to maintain. - The descendant inherits the ancestor’s
scripts, so you do not have to re-enter the code to add to the script. - You get consistency in the code and in the application
windows.
Using the example
Continuing with the example, the best way to build the window
to display customer information instead of employee information
is to build a window that is inherited from w_employee.
You change the title and modify the scripts, then save the descendent
window using a new name (such as w_customer).
To use inheritance to build a descendent window:
-
Click the Inherit button in the PowerBar.
The Inherit From Object dialog box displays.
-
Select the Windows object type at the bottom and
the library or libraries you want to look for the object in. -
Double-click the window you want to use to build
the new descendent window.or
Select the window and click OK.
The selected window displays in the Window painter workspace.
The title of the workspace indicates that the window is a
descendant and identifies the window from which it inherits its
definition (the ancestor window). -
Make the changes to the descendent window and
save the window with a new name.
What inherited objects contain
When you use inheritance to build an object, everything in
the ancestor object is inherited in all its descendants.
What you can do in the descendent window
You can:
- Change the properties
of the window - Add controls to the window and modify existing controls
- Size and position the window and the controls in
the window - Build new scripts for events in the window or its
controls - Reference the ancestor’s functions and
events - Reference the ancestor’s structures if
the ancestor contains a public or protected instance variable of
the structure data type - Access ancestor properties, such as instance variables,
if the scope of the property is public or protected - Extend or override inherited scripts
- Declare functions, structures, and variables for
the window - Declare user events for the window and its controls
What you cannot do
The only thing you cannot do is delete inherited controls.
Unneeded inherited controls If you don’t need an inherited control, you can make
it invisible in the descendent window.
Instance variables defined in an ancestor window
display in a descendent window
If
you create a window by inheriting it from an existing window that
has public or protected instance variables with simple data types,
the instance variables display and can be modified in the descendent
window’s Properties view. You’ll see them at the
bottom of the General tab page.
All public instance variables with simple data types such
as integer, boolean, character, date, string, and so on display.
Instance variables with the any or blob data
type or instance variables that are objects or arrays do not display.
About control names
PowerBuilder uses this syntax to show names of inherited controls:
1 |
<i>ancestorwindow::control</i> |
For example, in w_customer, which is inherited from
w_employee, if you open the property sheet for the Open
button, you see that its name displays as:
1 |
w_employee::cb_open |
Names of controls must be unique in an inheritance hierarchy.
For example, you cannot have a CommandButton named cb_close
defined in an ancestor and a different CommandButton named cb_close
defined in a child.
So you should develop a naming convention for controls in
windows that you plan to use as ancestors.
For more information
The issues concerning inheritance with windows are the same
as the issues concerning inheritance with user objects and menus. Chapter 10, “Understanding Inheritance “, describes the following
issues in detail:
- The basics of inheritance
- How to view the inheritance hierarchy
- Considerations when using inherited objects
- How to use inherited scripts
- How to call an ancestor script
- How to call an ancestor function