Rich thanks you for sharing...
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
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
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()
// no action
Example #2: Not Good OOD
// define different instance variables
// Forces you to reference different instance variables to compile and
// to not get null object reference.
choose case iuo_state.ClassName()
Disadvantages of dynamic calls
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)