Anda di halaman 1dari 2

OBJECTS IN POWERBUILDER

Basic objects in PowerBuilder -When you create a window, it is created within a running version of PB which has already created instantiated the window object from an internal window class. -All classes are derived from the PB superclass PowerObject and SystemFunctions. -In the Object browser : -An anti-clock arrow means that the Property/variable/event/function/structure is inherited -The number of squares show if it is public (3 squares), protected (2 squares), private (1 square) -Other restrictions such as read only are shown by half-colored squares. User Objects A user object is a custom object that PB provides to allow the developer to define objects that have attributes not available in the standard classes -Class or Non-visual user objects.- Utilized to encapsulate business logic Standard Class. Extend or override the functionality of standard PB non-visual objects. Transaction, datastore, pipeline, connection. PB provides 4 default global variables of the types: Transaction, dynamicdescriptionare etc. Custom Class. Provides an empty shell, derivated from the PB non-visual object C++ Class. To use DLL and C++ code. -Visual user Objects. To enhance the functionality of common PB visual controls or controls available from third parts. -Standard Visual. Derivated from the standard visual PB controls, example: CheckBox, CommandButton, ListBox Etc. -Custom Visual. Created by aggregating multiple controls or user objects. -External. They are visual user objects derived from custom controls created as Windows DLLs. VBX and OCX user objects. VBX are utilized only in PB 16 bits, OCX in 32 bits. Creating an instance of a class: open and create. Windows Open (WindowVariable {, ParentWindowVariable} ) -The last parameter is to create a child window wich exists within the boundaries of a parent window Open (WindowVariable, WindowType {,ParentWindowVariable} ) -When the windows that is open depends on a selection made by the user. OpenWithParm. To pass a parameter OpenSheet, OpenSheetWithParm. To open sheets in a MDI frame. Visual User Objects -To open a user object in a window at certain coordinates: -WindowName.OpenUserObject ( UserObject{,x,y}) -WindowName.OpenUserObject ( UserObject, ObjectType {,x,y}) -And OpenUserObjectWithParm in both formats -With Open and OpenuserObject you can specify the class of the window/VisualUserObject to be opened, making it possible to open a descendant different from the declarations object type. /*Keep a reference to the user object in the variable u_to_open*/ dragObject u_to_open string s_u_name s_u_name = sle_user.Text w_info.OpenUserObject(u_to_open, s_u_name, 100, 200) //Opening w-data_entry or any of its descendants w_data_entry w_data string s_window_name s_window_name = sle_win.Text Open(W_data, s_window_name) Create Using create you can do: Transaction g_TranForSybase G_TranForSybase = create Transaction or Transaction g_SomeTran String sValue = Transaction g_SomeTran = Create Using sValue But if Autoinstantiate is defined in the object, use the following: uo_GetPayroll luo_payroll // Not necessary: luo_payroll = create uo_GetPayroll Integrating User Objects in an application -With create and destroy: anyUserObject 1_anyUserObject l_AnyUserObject = Create anyUserObject l_anyUserObject,triggerEvent(xyz) // Some processing here destroy l_anyUserObject -With Create using powerObject l_anyUserObject string ls_className ls_className = anyUserObject l_anyUserObject = create using ls_className l_anyUserObject.triggerEvent(xyz) //Some code here destroy l_anyUserObject Destroying an Object -To destroy an instance, use the following: // Clears instance of window w_item_master Close (w_item_master) // Clears instance of user object uo_commandButton CloseUserObject (uo_CommandButton) // Clears the instance l_str_cls and remove it from memory Destroy l_str_cls Making reference to Objects - Using This by itself as a reference to the current object //Passing a DW control to a function in another user object uo_data.uf_retrieve(this) //Setting the most recently used DW control idw_currentdw = This - The Parent pronoun only works within dot notation. If you need a reference to the parent of an object, use the GetParent function. //Saving a reference to a controls parent window window w_save w_save = dw_1.GetParent ( ) //Passing a reference to the controls parent window to a function defined in a user object uo_winmgmt.uf_resize(This.GetParent ( ), 400, 600 ) - Other tools for getting the parent of an object include: ParentWindow property, used in a menu script to refer to the window that is the parent of the menu ParentWindow function, used in any script to get a reference to the window that is the parent of a particular window Abstract ancestor object. You define in the ancestor functions and properties that will be implemented in descendants. The abstract functions only have a return value. uo_empdata uo_emp1 string ls_objname // Establish wich descendant of uo_empdata to open ls_objname = . . . uo_empl = CREATE USING ls_objname //Function is declared in the ancestor class result = uo_empl.uf_special ( ) Object Windows -In PB a class is data type within the development environment. You can define variables to define instances of that class: w_item_master l_window_1 Open (l_window_1) // It opens different windows each time it is executed -Any object that you declare in a painter is declared global, then the following code will open always the same window Open(w_item_master) -To preserve references to multiple windows opened in a frame you can use an array: w_item_master I_item_master [ ] int InstanceNo = 1 OpenSheet (I_item_master [ I], ParentWindow, 1, & Cascaded!) InstanceNo++ -Opening multiple instances of a window: w_main w_1, w_2 open(w_1) open(w_2) or Window w_1, w_2

File: Object in PowerBuilder.sxw, 08/17/04

Open(w_1,w_main) Open(w_2,w_main) Structures and Autoinstantiated User Objects -When you assign an structure to another the whole structure is copied, when you assign and object variable to another, the object referecenceis copied.. -Autoinstantiated object behave like structures in assignment and declaration, you need to declare them to instantiate them.

1. 2. 3.

Define a public, user object-level function that takes as arguments the information needed from the window. Place the user object in the window When appropiate, call the function from the script in the Window, passing the needed information as arguments

Inheritance and function overloading -PB support inheritance for three objectt types: Menus Window User Objects -Communicating between a window and a Visual user object using events. -In a descendant window you can: Override, overload or extend ancestor scripts A custom visual object often requieres a user event, because you can write Access protected and public functions scripts only for events that occur in the user object itself; you cannot write events in the controls in the user object. Read or modify protected or public instance variables So you define user events for the user object and trigger those events in scripts Modify the properties of inherited controls for the controls contained in that user object; then in the Window painter, you -If you want to execute first the descendant script in a event: write scripts for the user events, referencing components of the window as Override the ancestor script needed. Place the new script Garbage Collection Use: Call Super::<Event Name> -Visual objects, Timing and shared objects are no garbage collected. -If in a descendent you want to run the original script do: -Functions to control garbage collection: Call Window_original::Event_name GarbageCollect, GarbageCollectGetTimeLimit, and -In Polymorphism you have different objects with functions of the same name. In GarbageCollectSetTimeLimit. function overloading you have one object with different versions of the same -Setting a larger time interval the garbage collection can be turned off (default = function. 0.5 Sec.) -Function overloading using inheritance: -The ancestor has a function, say: Read (string data1) -The descendant from that ancestor has a function called: Read (string data1, The MessageObject: integer data1) Handle Long Handle of the window control -When coding in the ancestor, the function Read will be chose according the Number UnsignedInt Event number (comes from Windows) parameters. Either from the ancestor or from the descendent itself LongParm Long Long parameter for the event, comes -An overloaded function is recognized by its signature: function name, arguments from windows and return value. DoubleParm Double A numeric value -Function Overloading without inheritance: String A string or string variable Here both functions are defined in the descendant, but with different parameters. StringParm Read (string data1) PowerObjectParm PowerObject Any PB object type Read (string data1, integer data2) Processed Boolean Indicator if the script processed the even (default: false) Composition ReturnValue Long The value you want return to window Composition is the inclusion in one class definition of another class definition. For (when True) When false this attribute example, in a window you can place a visual object, and on put another visual is ignored object, thus you will have the functionality of both in the same window. Encapsulation. -The best way to address variables defined in the user object is with functions: f_getVar1 - To return its value f_setVar1 - To initialize it -A standard user object should provide a initializing function to initialize its properties when it is instantiated Polymorphism. -User objects can have polymorphic behavior by using functions and events. -Function can be triggered or posted like events -The biggest difference between events and functions is that events can be extended at the descendant level, whereas functions cannot. PB objects communicate in three ways: -Global variables -Instance variable and functions Object_name,{type} {calltype}{when}FunctionName( Arg1, Arg2, ) -The message object OpenWithParm, OpenSheetWithParm, OpenUserObjectWithParm, CloseWithReturn User Objects. -Objects inherited from Custom Visual Objects have access to events at the user object level and not to events of controls that constitute it. <see a solution at pg 57 of instant objects) -Communicating between a window and a visual user object using funtions. -From a Window to a User Object -Using the messageObject to ensure a window doesnt close. Put the following code in the CloseQuery event of the window: Message.ReturnValue = 1 Objects as Structures -The advantages of using objects as structures is that with them you can: Create descendant objects and extended them Create functions to access the structure all at once Use access modifiers to limit access to certain instance variables Datastores - Consider creating a DataStore standard class user object to implement error checking and application-specific behavior for DataStores. Notes -En the window painter you can customize the painter bar or create a completely new toolbar to display all your standard visual user objects so that you can simply drag-and-drop them onto your windows. -User objects should be generic, scaleable and easly portable across different modules in the application and across different applications. -When writing in a descendant script, you can call user-defined functions from the ancestor with: Ancestorobject::function(arguments) -The global standard class user objects are: SQLCA, SQLDA,SQLSA,ERROR,MESSAGE

-From a User Object to a Window 1. Define a public, window-level function that takes as para meters the information needed from the user object. 2. Place the user object in the window. 3. When appropiate, call the function from a script in the user object, passing the needed information as parameters.

Summarized by Daniel Arenas from original Powerbuilder documentation

Anda mungkin juga menyukai