Booa8 Slide 09


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Booa8 Slide 09

    1. 1. Buliding Object-Oriented Applications in PowerBuilder Module 9: Methods
    2. 2. Objectives <ul><li>Describe the similarities and differences between events and functions </li></ul><ul><li>Understand how to override behavior </li></ul><ul><li>Understand function overloading </li></ul>
    3. 3. Topics <ul><li>Events and Functions as Methods </li></ul><ul><li>Availability </li></ul><ul><li>Invocation </li></ul><ul><li>Execution </li></ul><ul><li>Overriding Scripts </li></ul><ul><li>Overloading </li></ul><ul><li>Timing of Execution </li></ul><ul><li>Passing Arguments </li></ul><ul><li>Returning Values </li></ul><ul><li>Access </li></ul><ul><li>Accessing Ancestor Functions and Events in the Development Environment </li></ul><ul><li>Compiler Checking </li></ul>
    4. 4. Overview <ul><li>Three ways to add methods to an object: </li></ul><ul><ul><li>User-defined events </li></ul></ul><ul><ul><li>User-defined functions </li></ul></ul><ul><ul><li>Local external functions </li></ul></ul>
    5. 5. Availability of User-Defined Methods Class Application Menu User Object Window Window Control Object Function Yes Yes Yes Yes No Events Yes * Yes * Yes Yes Yes * New in PowerBuilder .0
    6. 6. Method Invocation <ul><li>Events and function syntax </li></ul><ul><li>{ ref_var }.{TRIGGER | post} {STATIC | dynamic} {FUNCTION | event} method_name ({ args }) </li></ul><ul><li>Examples: </li></ul><ul><ul><ul><li>w_invoice.of_SetCustomer(&quot; xxx &quot;) </li></ul></ul></ul><ul><ul><ul><li>w_invoice.EVENT ue_ResizeDW( ) </li></ul></ul></ul><ul><ul><ul><li>w_invoice.POST of_SetTotal(2031) </li></ul></ul></ul>
    7. 7. Invocation <ul><li>Alternate syntax for events: </li></ul><ul><ul><ul><li>{ ref_var }.TriggerEvent( enum ) </li></ul></ul></ul><ul><ul><ul><li>[ ref_var }.TriggerEvent( string ) </li></ul></ul></ul><ul><ul><ul><li>{ ref_var }.PostEvent( enum ) </li></ul></ul></ul><ul><ul><ul><li>[ ref_var }.PostEvent( string ) </li></ul></ul></ul><ul><li>Examples: </li></ul><ul><ul><ul><li>w_invoice.TriggerEvent(Clicked!) </li></ul></ul></ul><ul><ul><ul><li>w_cust.PostEvent(&quot;ue_retrieve&quot;) </li></ul></ul></ul>
    8. 8. Events Execution <ul><li>Scripts are built from the bottom up and executed from the top down: </li></ul>w_anc1 w_anc2 w_anc3 w_invoice {w_anc1} ue_Update( ) {w_anc1} ue_Update( ) {w_anc2} ue_Update( ) {w_anc3} ue_Update( ) w_invoice ue_Update( )
    9. 9. Function Execution <ul><li>Search from bottom up for a function with the same signature as the call: </li></ul><ul><ul><li>w_invoice.of_Save(TRUE) </li></ul></ul>{w_anc1} ue_Update( ) {w_anc1} of_Save(string) {w_anc2} of_Save(int) {w_anc3} of_Save(boolean) w_invoice of_Save( )
    10. 10. Overriding Ancestor Event Scripts Uncheck to override script
    11. 11. Overriding Ancestor Event Scripts <ul><li>Override can occur at any level </li></ul><ul><li>Execution ends with the lowest-level </li></ul><ul><li>script that has &quot;override&quot; selected </li></ul><ul><li>Example: w_anc3 sets override </li></ul><ul><li>for ue_update( ) — only w_anc3 </li></ul><ul><li>and w_invoice scripts are executed </li></ul>{w_anc1} ue_Update( ) {w_anc1} ue_Update( ) {w_anc2} ue_Update( ) {w_anc3} ue_Update( ) w_invoice ue_Update( )
    12. 12. Overriding Ancestor Function Scripts <ul><li>Define a function in a descendant with the same signature as the ancestor function </li></ul><ul><li>Descendent functions with the same signature take precedence (bottom-up search) </li></ul>
    13. 13. Overriding Ancestor Function Scripts <ul><li>Function defined in a descendant </li></ul><ul><li>with the signature of ancestor </li></ul><ul><li>function takes precedence </li></ul><ul><li>(bottom-up search) </li></ul>w_invoice.of_Save( ) {w_anc1} ue_Update( ) {w_anc1} of_Save( ) {w_anc2} of_Save(int) {w_anc3} of_Save(boolean) w_invoice of_Save( )
    14. 14. Extending Event Scripts <ul><li>Ancestor event scripts are extended in a descendant </li></ul>{w_anc1} ue_Update( ) {w_anc1} ue_Update( ) {w_anc2} ue_Update( ) {w_anc3} ue_Update( ) w_invoice ue_Update( )
    15. 15. Extending Functions <ul><li>Ancestor functions can be called from descendant object functions by specifying: </li></ul><ul><ul><ul><li>SUPER::< function > </li></ul></ul></ul><ul><li>and using the appropriate function signature </li></ul><ul><li>Search starts with immediate ancestor </li></ul><ul><li>To start the search at a particular level: </li></ul><ul><ul><ul><li>< ancestor_class_name >:: < function > </li></ul></ul></ul>
    16. 16. Overriding and Invoking Ancestor Script <ul><li>Use SUPER to execute ancestor </li></ul><ul><li>scripts after the current script </li></ul><ul><ul><li>Set Override Ancestor Script </li></ul></ul><ul><ul><li>Code descendent script </li></ul></ul><ul><ul><li>Invoke ancestor using SUPER </li></ul></ul><ul><ul><ul><li>SUPER::Event < event_method > </li></ul></ul></ul><ul><ul><li>Example: </li></ul></ul><ul><ul><ul><li>Super::event ue_update( ) </li></ul></ul></ul>{w_anc1} ue_Update( ) 2. {w_anc1} ue_Update( ) 3. {w_anc2} ue_Update( ) 4. {w_anc3} ue_Update( ) 1. w_invoice ue_Update( )
    17. 17. Function Overloading <ul><li>Use the same function name multiple times with different argument lists </li></ul><ul><li>At execution, PowerBuilder matches the signature of the function call against the signature of defined functions </li></ul><ul><li>In hierarchy, search is from the bottom up </li></ul><ul><li>Examples of PowerBuilder overloaded functions: </li></ul><ul><ul><ul><li>dw_data.GetItemNumber(li_row,'emp_id') </li></ul></ul></ul><ul><ul><ul><li>dw_data.GetItemNumber(li_row,1) </li></ul></ul></ul>
    18. 18. Function Overloading <ul><li>Declare two object-level functions: </li></ul><ul><ul><li>of_save( string as_action ) </li></ul></ul><ul><ul><li>of_save( boolean ab_action ) </li></ul></ul><ul><li>In a script, the first function is invoked with: </li></ul><ul><ul><ul><li>This.of_Save('No Commit') </li></ul></ul></ul><ul><li>The second function is invoked with: </li></ul><ul><ul><ul><li>This.of_Save(TRUE) </li></ul></ul></ul><ul><li>Functions can exist at any level in a hierarchy </li></ul>
    19. 19. Familiar Examples of Function Overloading <ul><li>For example, the MessageBox function is overloaded </li></ul>
    20. 20. Post Versus Trigger <ul><li>{ref_var}.{ TRIGGER | post } { STATIC | dynamic } & { FUNCTION | event } method_name ( {args}) </li></ul><ul><li>TRIGGER (default) </li></ul><ul><ul><li>Synchronous processing </li></ul></ul><ul><ul><li>Method executes immediately </li></ul></ul><ul><li>POST </li></ul><ul><ul><li>Must include keyword </li></ul></ul><ul><ul><li>Asynchronous processing </li></ul></ul>
    21. 21. Post Versus Trigger <ul><li>Example: </li></ul><ul><ul><li>w_invoice.Post of_Save( ) </li></ul></ul><ul><ul><li>w_cust.of_Save( ) </li></ul></ul><ul><li>The first function call will be executed after all other &quot;triggered&quot; methods and any prior &quot;posted&quot; methods </li></ul><ul><li>The second function will be the next method to be processed </li></ul>
    22. 22. Additional Considerations <ul><li>For a &quot;posted&quot; method: </li></ul><ul><ul><li>If the method returns a value, it will be ignored (no way to access it!) </li></ul></ul><ul><ul><li>It cannot be used as an operand in an expression or method argument </li></ul></ul><ul><ul><li>It cannot be used as an argument to another method </li></ul></ul>
    23. 23. Additional Considerations <ul><li>For unmapped events: </li></ul><ul><ul><li>If an event has arguments declared, then arguments must be supplied when &quot;calling&quot; the event </li></ul></ul><ul><ul><li>For example, if ue_insert is defined with one string argument, the first statement will not compile: </li></ul></ul><ul><ul><ul><li>This.Event ue_insert( ) //Bad </li></ul></ul></ul><ul><ul><ul><li>This.Event ue_insert(&quot;FIRST&quot;) //Good </li></ul></ul></ul>
    24. 24. TriggerEvent ( ) and PostEvent ( ) <ul><li>Used for events only </li></ul><ul><li>TriggerEvent(< string >) is synchronous </li></ul><ul><li>PostEvent(< string >) is asynchronous </li></ul><ul><li>Use a string variable to change execution-time processing </li></ul><ul><li>Caution: Arguments cannot be passed for user-defined events with arguments </li></ul>
    25. 25. Defining Event Arguments — Review <ul><li>System, mapped, and custom event arguments: </li></ul><ul><ul><li>Defined by PowerBuilder </li></ul></ul><ul><ul><li>Cannot be changed </li></ul></ul><ul><li>Unmapped events: </li></ul><ul><ul><li>Defined in event painter </li></ul></ul>
    26. 26. Return Values <ul><li>System, custom, and mapped events always return long and cannot be changed </li></ul><ul><li>Unmapped events can return any data type and corresponding value </li></ul><ul><li>If multiple values must be returned from an event or function, use variable arguments passed by reference </li></ul>
    27. 27. Methods During Development <ul><li>Ancestor event scripts can be viewed within the PowerScript painter </li></ul><ul><li>Ancestor functions cannot be accessed when editing a descendant </li></ul><ul><li>Browser provides access to all method names, argument lists, and return data type </li></ul><ul><li>Ancestor objects cannot be edited while editing a descendant </li></ul>
    28. 28. Bypassing Compiler Validation <ul><li>{ref_var}.{ TRIGGER | post } </li></ul><ul><li>{ STATIC | dynamic } </li></ul><ul><li>{ FUNCTION | event } name ( {args}) </li></ul><ul><li>STATIC validates at compile time (and during execution) </li></ul><ul><li>DYNAMIC bypasses compiler validation </li></ul>
    29. 29. Summary <ul><li>Descendent events extend ancestor scripts by default. </li></ul><ul><li>Functions can be overloaded. </li></ul><ul><li>A function's signature is used to invoke the appropriate function. </li></ul><ul><li>At most, one function is automatically executed — the first one with a matching signature. </li></ul>
    30. 30. Summary <ul><li>Access to functions can be restricted by making them private or protected. </li></ul><ul><li>Events and functions can execute synchronously or asynchronously. </li></ul><ul><li>Events and functions can be invoked statically or dynamically. </li></ul><ul><li>TriggerEvent( ) and PostEvent( ) provide the ability to pass strings as event names. </li></ul>
    31. 31. Summary Questions