Booa8 Slide 11


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 11

    1. 1. Buliding Object-Oriented Applications in PowerBuilder Module 11: Messaging Basics
    2. 2. Objectives <ul><li>Describe how objects communicate with each other </li></ul><ul><li>Define direct and generic referencing </li></ul><ul><li>Use pronouns </li></ul><ul><li>Explain how to pass data between objects </li></ul>
    3. 3. Topics <ul><li>M essages Defined </li></ul><ul><li>Method Resolution </li></ul><ul><li>Uses of Messages </li></ul><ul><li>References </li></ul><ul><li>PowerBuilder Functions That Return References </li></ul><ul><li>Reference Properties </li></ul><ul><li>Generic Referencing </li></ul><ul><li>Passing Information </li></ul>
    4. 4. Messages — Definition <ul><li>In object orientation, communication is performed via messages </li></ul><ul><li>A message contains: </li></ul><ul><ul><li>A reference to the “receiving” object </li></ul></ul><ul><ul><li>The name of a “receiver” object method </li></ul></ul><ul><ul><li>Values for arguments (optional) </li></ul></ul><ul><ul><li>An assignment for a return value (optional) </li></ul></ul>
    5. 5. Messages — Definition <ul><li>Examples: </li></ul><ul><ul><li>Events: </li></ul></ul><ul><ul><ul><li>li_rv = w_invoice.EVENT ue_Save( ) </li></ul></ul></ul><ul><ul><ul><li>li_rv = w_invoice.TriggerEvent(“ue_Save”) </li></ul></ul></ul><ul><ul><li>Functions: </li></ul></ul><ul><ul><ul><li>ll_success = w_invoice.of_Update( ) </li></ul></ul></ul><ul><ul><ul><li>li_rv = w_cust.of_Retrieve( ) </li></ul></ul></ul>
    6. 6. Method Resolution <ul><li>If the object reference is omitted, PowerBuilder must resolve the method to invoke; the results may be unexpected </li></ul><ul><li>Static methods verified at compile time </li></ul><ul><li>Dynamic methods resolved at execution time </li></ul><ul><li>Search process different for events and functions </li></ul>
    7. 7. Method Resolution for Functions <ul><li>If a reference is not provided, the search order is: </li></ul><ul><ul><li>Global external functions </li></ul></ul><ul><ul><li>Global functions </li></ul></ul><ul><ul><li>Current instance (object functions and local external functions) </li></ul></ul><ul><ul><li>Ancestor(s) of the current instance </li></ul></ul>
    8. 8. Method Resolution for Events <ul><li>If a reference is not provided, the search order is: </li></ul><ul><ul><li>Current instance </li></ul></ul><ul><ul><li>Parent object </li></ul></ul><ul><ul><li>Parent ancestors </li></ul></ul><ul><ul><li>From a control when TriggerEvent( ) / PostEvent( ) is used, search is confined to the control </li></ul></ul><ul><li>Providing a reference is more efficient and restricts the possibilities </li></ul>
    9. 9. Uses of Messages <ul><li>Messages between objects can: </li></ul><ul><ul><li>Request data </li></ul></ul><ul><ul><li>Pass data </li></ul></ul><ul><ul><li>Request services </li></ul></ul><ul><ul><li>Initiate services </li></ul></ul>
    10. 10. Message Example <ul><li>&quot;Customer List&quot; CustomerDetail.of_ShowDetails(102) </li></ul>
    11. 11. References for Messages <ul><li>Direct referencing </li></ul><ul><li>Pronouns </li></ul><ul><li>PowerBuilder functions </li></ul><ul><li>Class reference properties </li></ul><ul><li>Generic referencing </li></ul>
    12. 12. Message Using Direct Reference <ul><li>Reference variable used during instantiation </li></ul><ul><li>Window example: </li></ul><ul><ul><li>w_cust lw_cust </li></ul></ul><ul><ul><li>Open(lw_cust) </li></ul></ul><ul><ul><li>lw_cust.of_SeqWrite( ) </li></ul></ul>
    13. 13. Message Using Direct Reference <ul><li>Window control example: </li></ul>$ PBExportHeader$w_cust.srw $ PBExportComments$Display customer details forward global type w_cust from Window end type type dw_data from datawindow within w_cust end type end forward ... global type w_cust from Window int X=836 int Y=372 int Width=1249 ... dw_data dw_data end type global w_cust w_cust ... on w_cust.create this.dw_data=create dw_data this.Control[]={ this.dw_data} end on ... on w_cust.destroy destroy(this.dw_data) end on Define subclass Define instance variable Instantiate and assign pointer Destroy
    14. 14. Message Using Direct Reference <ul><li>Can &quot;directly&quot; reference the control within any script in the window: </li></ul><ul><ul><ul><li>dw_data.Retrieve(102) </li></ul></ul></ul><ul><li>dw_data: </li></ul><ul><ul><li>Is an instance variable (property) of the window </li></ul></ul><ul><ul><li>Data type is dw_data </li></ul></ul><ul><ul><li>Is a reference for class dw_data that is instantiated at execution time </li></ul></ul><ul><ul><li>Class dw_data is inherited from the system class DataWindow (control) </li></ul></ul>
    15. 15. Using Pronouns and Properties in Messages <ul><li>Keywords in PowerBuilder </li></ul><ul><li>Use to generically reference instances </li></ul><ul><ul><li>This </li></ul></ul><ul><ul><li>Parent </li></ul></ul><ul><ul><li>ParentWindow (menu property) </li></ul></ul>
    16. 16. Messaging with the Pronoun This <ul><li>Refers to the object that contains the message (in a script command) </li></ul><ul><li>Avoids problems in variable / function resolution </li></ul><ul><li>Can generically call a function without &quot;hard coding&quot; a reference variable: </li></ul><ul><ul><ul><li>Close(This) </li></ul></ul></ul><ul><ul><ul><li>This.Resize(20,30) </li></ul></ul></ul><ul><ul><ul><li>w_cust.of_Update(This) </li></ul></ul></ul>
    17. 17. Messaging with the Pronoun Parent <ul><li>Refers to the object that &quot;contains&quot; the object that contains the message (in a script command) </li></ul><ul><li>Standard control in a window, for example: </li></ul><ul><ul><ul><li>Parent.of_Update(This) </li></ul></ul></ul><ul><li>Control in a custom visual object, for example: </li></ul><ul><ul><ul><li>Parent.of_CloseParent( ) </li></ul></ul></ul><ul><li>Parent of a menu item is the level immediately &quot;above&quot; it: </li></ul><ul><ul><ul><li>Parent.Hide( ) </li></ul></ul></ul>
    18. 18. Messaging with the Property ParentWindow <ul><li>Used in menu scripts to refer to the window with which the menu is associated </li></ul><ul><li>Data type is window </li></ul><ul><li>To trigger an event from a menu to its window, use either of the following: </li></ul><ul><ul><ul><li>ParentWindow.TriggerEvent(&quot;ue_Save&quot;) </li></ul></ul></ul><ul><ul><ul><li>ParentWindow.EVENT DYNAMIC ue_Save( ) </li></ul></ul></ul>
    19. 19. Pronoun and Property Restrictions <ul><li>Cannot use dot notation to combine pronouns </li></ul><ul><ul><li>These statements will not compile: </li></ul></ul><ul><ul><ul><li>This.Parent.x = 7 </li></ul></ul></ul><ul><ul><ul><li>Parent.Parent.Dynamic of_Save( ) </li></ul></ul></ul><ul><li>ParentWindow can refer to only the properties and methods of the window it references — not controls in the window </li></ul><ul><ul><li>This statement will not compile: </li></ul></ul><ul><ul><ul><li>ParentWindow.cb_ok.enabled = TRUE </li></ul></ul></ul>
    20. 20. Functions That Return References <ul><li>A number of functions return references to objects and can be used for sending messages </li></ul><ul><li>Here is a partial list: </li></ul><ul><ul><li>ParentWindow( )  GetActiveSheet( ) </li></ul></ul><ul><ul><li>GetFirstSheet( )  GetNextSheet( ) </li></ul></ul><ul><ul><li>GetFocus( )  GetApplication( ) </li></ul></ul><ul><ul><li>GetParent( )  DraggedObject( ) </li></ul></ul>
    21. 21. References as Properties <ul><li>Some classes have properties that are reference variables for other objects </li></ul><ul><ul><li>MenuID – Property of window </li></ul></ul><ul><ul><li>Control[ ] – Property of window, CVUO, and tab </li></ul></ul><ul><ul><li>Item[ ] – Property of menu and menu item </li></ul></ul>
    22. 22. MenuID Property of Window <ul><li>MenuID window property </li></ul><ul><ul><li>Variable of data type menu </li></ul></ul><ul><ul><li>Populated when window is opened, if menu name is specified </li></ul></ul><ul><li>Window menu property has name of menu (string) </li></ul><ul><li>Is there a menu for this window? </li></ul><ul><li> menu lm_temp </li></ul><ul><li>lm_temp = This.MenuID </li></ul><ul><li>IF IsValid(lm_temp) THEN ... </li></ul>
    23. 23. Control[ ] Property of Windows and CVUOs <ul><li>Array of reference variables </li></ul><ul><li>Data type is windowcontrol </li></ul><ul><li>Populated when window or CVUO is created </li></ul><ul><li>Each element references a control within the parent object </li></ul>
    24. 24. Control[ ] Property of Windows and CVUOs <ul><li>Exported syntax for window: </li></ul><ul><li>on w_cust.CREATE This.dw_detail = CREATE dw_detail This.dw_list = CREATE dw_list This.cb_1 = CREATE cb_1 This.Control[ ] = {This.dw_detail, this.dw_list, This.cb_1} end on </li></ul>
    25. 25. <ul><li>Data type of userobject </li></ul><ul><li>Contains a reference for each tab page within the tab control: </li></ul><ul><ul><li>global uo_test_tabs uo_test_tabs </li></ul></ul><ul><ul><li>on uo_test_tabs.create </li></ul></ul><ul><ul><li>this.tab_3=create tab_3 </li></ul></ul><ul><ul><li>this.tab_2=create tab_2 </li></ul></ul><ul><ul><li>this.tab_1=create tab_1 </li></ul></ul><ul><ul><li>this.Control[]={this.tab_3, this.tab_2, this.tab_1} </li></ul></ul><ul><ul><li>end on </li></ul></ul>Control[ ] Property of Tab
    26. 26. Item[ ] Property of Menu and Menu Item <ul><li>Contains a reference for each menu item in the next lower level: </li></ul><ul><li>Can generically search for menu items with specific property values or set property values (for example, Visible) </li></ul><ul><ul><li>li_max = UpperBound(This.item) </li></ul></ul><ul><ul><li>FOR li_ndx = 1 TO li_max </li></ul></ul><ul><ul><li>This.item[li_ndx].Visible=TRUE </li></ul></ul><ul><ul><li>... </li></ul></ul>
    27. 27. Generic Referencing <ul><li>Can use: </li></ul><ul><ul><li>Variables declared from an ancestor class </li></ul></ul><ul><ul><li>Reference values returned by functions </li></ul></ul><ul><ul><li>Pronouns </li></ul></ul><ul><li>For reference variable data types corresponding to an ancestor class: </li></ul><ul><ul><li>Only properties of the ancestor class are available for the referenced object </li></ul></ul><ul><ul><li>Only dynamically invoked methods of the descendent class can be used </li></ul></ul>
    28. 28. Referencing Using Ancestor Data Types <ul><li>Example: </li></ul><ul><ul><li>window lw_temp </li></ul></ul><ul><ul><li>Open(w_invoice) </li></ul></ul><ul><ul><li>lw_temp = w_invoice </li></ul></ul><ul><li>Only properties of the Window system class can be used with lw_temp </li></ul><ul><li>Only dynamically invoked methods can be used with lw_temp </li></ul>
    29. 29. Referencing Using Ancestor Data Types <ul><li>The instance can be referenced using either variable: </li></ul><ul><ul><ul><li>window lw_temp1 </li></ul></ul></ul><ul><ul><ul><li>lw_temp1 = w_invoice </li></ul></ul></ul>Local memory Global memory w_invoice w_invoice w_invoice Class pool window lw_temp1 instance of w_invoice Instance pool
    30. 30. Generic Referencing Example <ul><li>Nested CVUOs can send a message to the window container </li></ul><ul><li>Example: Close the window from a command button in a nested CVUO: </li></ul><ul><ul><li>// Function: of_Close( ) </li></ul></ul><ul><ul><li>GraphicObject lgr_temp </li></ul></ul><ul><ul><li>lgr_temp=This.GetParent( ) </li></ul></ul><ul><ul><li>DO WHILE TypeOf(lgr_temp) <> Window! </li></ul></ul><ul><ul><li> lgr_temp = lgr_temp.GetParent( ) </li></ul></ul><ul><ul><li>LOOP </li></ul></ul><ul><ul><li>Close(lgr_temp) </li></ul></ul>
    31. 31. Generic Referencing Example <ul><li>For a CommandButton in a CVUO to close the window container, use: </li></ul><ul><ul><li>Parent.of_Close( ) </li></ul></ul><ul><li>Notice that no reference is made to a specific object or class </li></ul>
    32. 32. Passing Data to a Method <ul><li>Data can be passed by: </li></ul><ul><ul><li>Value </li></ul></ul><ul><ul><li>Reference </li></ul></ul><ul><ul><li>ReadOnly </li></ul></ul><ul><li>Different behavior for standard data types versus object reference variables </li></ul>
    33. 33. Passing Data to a Method <ul><li>By value for standard data types: </li></ul><ul><ul><li>A copy is passed to the method </li></ul></ul><ul><ul><li>Changes cannot be made to the original value </li></ul></ul><ul><li>By value for object references: </li></ul><ul><ul><li>A copy of the pointer is passed to the method </li></ul></ul><ul><ul><li>Changes can be made to properties of the object </li></ul></ul><ul><ul><li>Exception: For structures, a copy of the structure is passed </li></ul></ul>
    34. 34. Passing Data to a Method <ul><li>By reference , for all data types: </li></ul><ul><ul><li>Method receives a pointer to the argument </li></ul></ul><ul><ul><li>Changes can be made to the original value or object properties </li></ul></ul>
    35. 35. Passing Data to a Method <ul><li>By ReadOnly for standard data types: </li></ul><ul><ul><li>Pointer to the data is passed </li></ul></ul><ul><ul><li>Data value cannot be changed </li></ul></ul><ul><li>By ReadOnly for object references: </li></ul><ul><ul><li>Copy of reference </li></ul></ul><ul><ul><li>Reference cannot be changed </li></ul></ul><ul><ul><li>Values can change (even for structures) </li></ul></ul>
    36. 36. Passing Variable Number of Arguments <ul><li>Three ways to pass multiple values in one argument to a method: </li></ul><ul><ul><li>Structures </li></ul></ul><ul><ul><li>Custom user classes </li></ul></ul><ul><ul><li>Any data type </li></ul></ul>
    37. 37. Structures <ul><li>Can pass a variable number of arguments of more than one data type </li></ul><ul><li>Define a structure of unbounded arrays: </li></ul><ul><ul><ul><li>str_pass lstr_pass </li></ul></ul></ul><ul><ul><ul><li>lstr_pass.i_arg [1] = 1 </li></ul></ul></ul><ul><ul><ul><li>lstr_pass.s_arg [1] = &quot;Sam&quot; </li></ul></ul></ul><ul><ul><ul><li>Parent.of_SeqWrite(lstr_pass) </li></ul></ul></ul>
    38. 38. Custom Class Versus Structure <ul><li>Define a custom class with properties like those of structure ( without AutoInstantiate): </li></ul><ul><ul><ul><li>n_cust ln_cust </li></ul></ul></ul><ul><ul><ul><li>ln_cust = CREATE n_cust </li></ul></ul></ul><ul><ul><ul><li>ln_cust.is_name = &quot;Sam&quot; </li></ul></ul></ul><ul><ul><ul><li>ln_cust.ii_key = 1 </li></ul></ul></ul><ul><ul><ul><li>This.of_SeqWrite(ln_cust) </li></ul></ul></ul>
    39. 39. Custom Class Versus Structure <ul><li>Define a custom class with properties like those of structure ( with AutoInstantiate) </li></ul><ul><ul><ul><li>n_cust ln_cust </li></ul></ul></ul><ul><ul><ul><li>ln_cust.is_name = &quot;Sam&quot; </li></ul></ul></ul><ul><ul><ul><li>ln_cust.ii_key = 1 </li></ul></ul></ul><ul><ul><ul><li>This.of_SeqWrite(ln_cust) </li></ul></ul></ul><ul><li>Unlike structures, methods are supported (regardless of AutoInstantiate) </li></ul>
    40. 40. Using the Any Data Type <ul><li>Generic data type </li></ul><ul><li>Can hold value for standard data type or a data type corresponding to a class </li></ul><ul><li>Uses standard assignment statement </li></ul><ul><ul><ul><li>int li_hold = 7 </li></ul></ul></ul><ul><ul><ul><li>any la_hold </li></ul></ul></ul><ul><ul><ul><li>la_hold = li_hold </li></ul></ul></ul>
    41. 41. Using the Any Data Type <ul><li>Use ClassName( ) to determine the data type passed: </li></ul><ul><ul><li>// Function of_Save(any aa_pass) </li></ul></ul><ul><ul><ul><li>int li_hold </li></ul></ul></ul><ul><ul><ul><li>boolean lb_hold </li></ul></ul></ul><ul><ul><ul><li>CHOOSE CASE ClassName(aa_pass) </li></ul></ul></ul><ul><ul><ul><li>CASE “integer” </li></ul></ul></ul><ul><ul><ul><li>li_hold = aa_pass </li></ul></ul></ul><ul><ul><ul><li>... </li></ul></ul></ul><ul><ul><ul><li>CASE “boolean” </li></ul></ul></ul><ul><ul><ul><li>lb_hold = aa_pass </li></ul></ul></ul><ul><ul><ul><li>... </li></ul></ul></ul><ul><ul><ul><li>END CHOOSE </li></ul></ul></ul>
    42. 42. Using the Any Data Type <ul><li>Can use when data type of argument can vary </li></ul><ul><li>Must reassign to proper data type before using </li></ul><ul><li>More resources required </li></ul><ul><li>Compiler cannot perform normal error checking </li></ul>
    43. 43. Summary <ul><li>A message is a communication between objects. </li></ul><ul><li>A message consists of the address of the target object, the name of a method, and, optionally, information passed as values of arguments. </li></ul><ul><li>Messages can be used to request data, pass data, request services, and initiate services. </li></ul><ul><li>Direct messaging uses the reference variable that was used in the instantiation process. </li></ul>
    44. 44. Summary <ul><li>The pronouns This and Parent and the property ParentWindow are used to generically send a message. </li></ul><ul><li>Some functions return a reference to an object that can be used in messaging. </li></ul><ul><li>Some classes have properties that can be used for referencing. </li></ul>
    45. 45. Summary <ul><li>Arguments can be passed by value, reference, or as read-only. </li></ul><ul><li>A variable number and type of arguments can be passed using structures, custom classes, and the Any data type as function arguments. </li></ul>
    46. 46. Summary Questions