Using inherited scripts – PB Docs 70

Using inherited scripts

In the hierarchy formed by ancestors and descendants, each
descendant inherits its event scripts from its immediate ancestor.
If an inherited event does not have a script, you can write a script
for the event in the descendant. If the inherited event does have
a script, the ancestor script will execute in the descendant unless
you extend the script or override it. You can:

  • Extend the ancestor
    script–build a script that executes after
    the
    ancestor script
  • Override the ancestor script–execute a
    descendent script instead
    of the ancestor script

You cannot delete or modify an ancestor script from within
a descendant.

Executing code before the ancestor script

To write a script that executes before
the
ancestor script, first override the ancestor script and then in
the descendent script explicitly call the ancestor script at the
appropriate place.

For more information, see “Calling an ancestor script”.

Getting the return value of the ancestor script

To get the return value of an ancestor script, you can use
the AncestorReturnValue variable. This variable is always available
in descendent scripts that extend an ancestor script. It is also
available if you override the ancestor script and use the CALL syntax
to call the ancestor event script.

For more information, see Application
Techniques

.

Viewing inherited scripts

If an inherited object or control has a script defined only
in an ancestor, no script displays in the Script view: you do not
see the ancestor script.

The Script view indicates which events have scripts written
for an ancestor as follows:

  • If the event has a
    script in an ancestor only, the script icon next to the event name
    in the second dropdown listbox is displayed in color.
  • If the event has a script in an ancestor as well
    as in the object you are working with, the script icon is displayed
    half in color.

proc.gif To view an ancestor script:

  1. (For visual objects) Select the object
    or control whose ancestor script you want to view.

    For example, to view an inherited script for a window, make
    sure no control is selected.

  2. (For visual objects) Select Script from the object
    or control’s popup menu to display the Script view for
    that object or control.

  3. In the Script view, select the event whose script
    you want to see in the second dropdown listbox.

    The Script view does not display the script for the ancestor.
    No script displays.

  4. In the third dropdown listbox in the Script view,
    select the parent ancestor object name below the object name.

    The Script view displays any script defined in the ancestor
    object immediately above the current object in the hierarchy.

  5. To climb the inheritance hierarchy, in the third
    dropdown listbox select the script for the grandparent of the current
    object, great-grandparent, and so on until you display the scripts
    you want.

    The Script view displays the scripts for each of the ancestor
    objects. You can traverse the entire inheritance hierarchy using
    the third dropdown listbox.

Extending a script

When you extend an ancestor script for an event, PowerBuilder executes
the ancestor script, then
executes the script
for the descendant when the event is triggered.

proc.gif To extend an ancestor script:

  1. (For a visual object) Select the object
    or control whose ancestor script you want to extend.

    To extend a script for a window, make sure no control is selected.

  2. (For a visual object) Select Script from the object
    of control’s popup menu to display the Script view for
    that object or control.

    The Script view does not display the script for the ancestor.
    No script displays.

  3. In the second dropdown listbox in the Script view,
    select the event for which you want to extend the script.

  4. Be sure that Extend Ancestor Script on the Edit
    menu or the popup menu is selected.

    Extending
    the ancestor script is the default.

  5. In the Script view, enter the appropriate statements.

    You can call the script for any event in any ancestor as well
    as call any user-defined functions that have been defined for the
    ancestor.

    For information about calling an ancestor
    script or function, see “Calling an ancestor script” and “Calling
    an ancestor function”
    .

Example of extending a script

If the ancestor window script for the Clicked event in a button
beeps when the user clicks the button without selecting an item
in a listbox, you might extend the script in the descendant to display
a message box in addition to beeping.

Overriding
a script

proc.gif To override an ancestor script:

  1. (For a visual object) Select the object
    or control whose ancestor script you want to override.

    To override a window-level script, make sure no control is
    selected.

  2. (For a visual object) Select Script from the object
    or control’s popup menu to display the Script view for
    that object or control.

  3. In the second dropdown listbox in the Script view,
    select the event for which you want to override the script.

  4. Select Extend Ancestor Script on the Edit menu
    or the popup menu to clear the checkmark.

    You do not want Extend Ancestor Script selected. Not extending
    the ancestor script means that you are overriding the script.

  5. Code a script for the event in the descendant.

    You can call the script for any event in any ancestor as well
    as call any user-defined functions that have been defined for the
    ancestor.

    For information about calling an ancestor
    script or function, see “Calling an ancestor script” and “Calling
    an ancestor function”
    .

    note.gif Override but not execute To override a script for the ancestor but not execute a script
    in the descendant, enter only a comment in the workspace.

During execution, PowerBuilder executes the descendent script
when the event is triggered. The ancestor script is not executed.

Example of overriding a script

If the script for the Open event in the ancestor window displays
employee files and you want to display customer files in the descendent
window, select Override Ancestor Script and create a new script
for the Open event in the descendant to display customer files.

Calling an ancestor script

When you write a script for a descendent object or control,
you can call scripts written for any ancestor. You can refer by
name to any ancestor of the descendent object in a script, not just
the immediate ancestor (parent).

For more information about calling scripts
for an event in an ancestor window, user object, or menu, see the PowerScript
Reference

.

To reference the immediate ancestor (parent), you can use
the Super reserved word.

For more information about Super, see the PowerScript
Reference

.

Callingan ancestor function

When you write a script for a descendent window, user object,
or menu, you can call user-defined functions that have been defined
for any of its ancestors.

To call the first function up the inheritance hierarchy, just
call the function as usual:

If there are several versions of the function up the inheritance
hierarchy and you don’t want to call the first one up,
you need to specify the name of the object defining the function
you want:

note.gif Limitation The second syntax, in which you directly specify the ancestor
object, works only in scripts for the descendent object itself,
not in scripts for controls or user objects
in
the descendent object or in menu item scripts.

To call a specific version of an ancestor user-defined function
in a script for a control, user object, or menu item in a descendent
object, do the following:

  1. Define an object-level
    user-defined function in the descendent object that calls the ancestor
    function.
  2. Call the function you just defined in the descendent
    script.

For more information about calling an ancestor
function, see the PowerScript Reference
.


Document get from Powerbuilder help
Thank you for watching.
Was this article helpful?
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x