PowerBuilder and Object Oriented Programming

I’ve seen too many articles mentioning PowerBuilder and Microsoft Access or Microsoft Visual Basic in the same sentence, so took it upon myself to spread the word about some of the more advanced OOP techniques supported by PowerBuilder.   I’ll post them as I come across them in my work.  The first is dynamic function calls or dynamic events.

Dynamic Calls in PowerBuilder

When you specify a dynamic call in PowerBuilder, the function or event does not have to exist when you compile the code. You are indicating to the compiler that there will be a suitable function or event available at execution time.   For a dynamic call, PowerBuilder waits until it is time to execute the function or event to look for it.  This gives you flexibility and allows you to call functions or events in descendants that do not exist in the ancestor.
Another type of dynamic call is when you use the object dot notation, like in a dataobject or OLE object such as a Browser control or Map Control.

Why would you want to use dynamic calls?

To better support Polymorphism and good OOD.  Why would you want to do that?  One example might be a TreeView that displays a different ListView that is dependent on the type of branch clicked on in the tree.  Let’s say the tree view represented Continents, Countries and Cities and you wish to display a different information depending on which type was clicked.  If the user clicks Continents then you show a list of Countries, and if the user clicks Countries you show a list of Cities for the Country and for some strange business reason the user would only print lists of Countries or Cities but never a single city.
A good design would be to create one user object with shared functionality, then inherit from it to create three different specific versions each for the different display types.  You my have functions or events specific to one or more types (Continent and Country) but not all of them have the function or you just wish to use solid polymorphic and encapsulated oo design.   Since you are creating your list view user object on demand you’ll need to CREATE it and assign to an instance variable to reference it in memory.   But without dymanic function calls it makes things more difficult as you’d need to define your instance variable of a type more specific than you might want to so that it has the event or function definition and will compile.
Assume the following object hierarchy
1. userobject
2. uo_place
3. uo_continent, uo_country,  uo_city
Say uo_city is the only user object to NOT have an event ue_print( ) and the uo_place ancestor does not have the function either.
Example #1:   Better OOD
userobject iuo_listview
iuo_listview.EVENT ue_print()  - Won't compile, no such function exists on type userobject.
iou_listview.EVENT dynamic ue_print() - Will compile, but will fail at runtime if doesn't exist.

//  With this setup your code can reference one instance variable
//  but it needs to be aware of the object type to runtime errors.
choose case iuo_listview.ClassName()
case  'uo_state', 'uo_country'
  iuo_listview.EVENT dynamic ue_print()
case else
  // no action
end choose
Example #2:  Not Good OOD
// define different instance variables
uo_state iuo_state
uo_continent iuo_continent
uo_country iuo_country

// Forces you to reference different instance variables to compile and
// to not get null object reference.
choose case iuo_state.ClassName()
case 'uo_state'
  iuo_state.EVENT ue_print()
case 'uo_continent'
  iuo_continent.EVENT ue_print()
case else
  // nothing
end choose
Disadvantages of dynamic calls
Slower performance
Because dynamic calls are resolved at runtime, they are slower than static calls. If you need the fastest performance, design your application to avoid dynamic calls.  (the performance penalty is negligible unless you are calling events/functions repetitively)
Less error checking
When you use dynamic calls, you are foregoing error checking provided by the compiler. Your application is more open to application errors, because functions that are called dynamically might be unavailable at execution time. Do not use a dynamic call when a static call will suffice.
update 11/29/2013: Question: There is a question asking if inheritance can be implemented in C# with some code as an example. I am going to answer the question in a new blog post here.
Rich (aka DisplacedGuy)

6 Responses

  1. Hello:

    I have a large app. that use Dynamic Object Creation, in my case I have NVO’s that have general functionally and using inheritance I’m able to extend business functionalyty and call it dynamically.. like this:

    nvo_base_bc cust_rule

    SELECT rule_name
    INTO :cust_rule_Name
    FROM business_rules_customer WHERE cust_id = :cust_id_var;

    cust_rule = CREATE USING cust_rule_Name

    … In this Case Validation() and CustomRule() method must be in nvo_base_bc and in the class to be instanciated… the power of this is inheritance… you can call base methods and extended methods from your Dynamic Class.

    Well… the point is: Can I do that on C#?

  2. Greetings from Utah! I’m bored at work so I decided to check out your website on my iphone during lunch break. I love the knowledge you present here and can’t wait to take a look when I get home. I’m surprised at how quick your blog loaded on my cell phone .. I’m not even using WIFI, just 3G .. Anyhow, good site!

Leave a Reply

Your email address will not be published. Required fields are marked *