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

Booa8 Slide 11

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