Related Topics: PowerBuilder

PowerBuilder: Article

Getting to the Top of the Pecking Order

Instantiating service NVOs first

"Chicken flocks have a well-defined hierarchy called a pecking order. A chicken at the top of the pecking order gets to do the things she wants by 'pushing the others around' a little, giving them a short, sharp peck if they don't submit to her, allowing her to go where she wants to go or access food and drink first. Chickens naturally have a pecking order to ensure they live in harmony. When food is available there are no fights. Everyone lives in peace.

The bird at the top of the pecking order will have first access to water, food, the best roosting place and so on. The bird at the bottom of the pecking order has the least ‘rights' in the flock and will usually be the last to the food and will ‘skirt' around food that is scattered for them, nipping in to grab a beak full when possible. She (or sometimes he) has last rights to food and other resources."

What's pecking order got to do with PowerBuilder? When does pecking order matter in PowerScript coding? Read on to see the connection!

PowerBuilder Scenario
Your application has a window containing custom visual user objects (CVUOs) that require system services in their constructor events. Obviously, the services must be resident and active before the CVUO constructor is invoked. Using globals (functions, or NVO references) is one approach, but you want to code your solution in a Service Based Object Oriented manner. Being an object-oriented aficionado, you don't want the service to be of global scope. You'd rather define the NVO as a Window service. You define the service inside a Custom Class User Object (NVO) that you declare on the window and instantiate in the Open event. The window defines a NVO instance variable and creates it in its Open event. The CCUO calls the NVO service via the window in its constructor event. Figure 1 shows the code objects in the system tree. Figure 2 illustrates the scenario.

Figure 1: Code objects

Figure 2: CVUO in a Window calling service in constructor

The problem is that the NVO is not created until after the CCUOs. At runtime, because CVUO control constructors are invoked before the service is instantiated, a Null Object Error will be thrown. Figure 3 shows the Window event sequence. Notice that number 3, the NVO constructor, follows number 1, the CCUO constructor. In other words the NVO is lower down in the pecking order. How can we move the NVO up to number 1? After all, the open event always occurs after all the controls are created? I'll show you three possible solutions. You pick the one that suits your taste best.

Figure 3: NVO lower down in the pecking order

Solution 1: Deferring CCUO Constructor code
I term the first solution "Classic" because it relies on a technique that has been around since the earliest versions. The solution relies on Post vs. Trigger event invocation timing. Every PowerBuilder programmer has seen this technique used in the Window open event cascade. You add an event that you usually call ue_postconstructor to the CCUO, code a this.PostEvent(   ) to it in the constructor, and move the timing-sensitive code from the constructor into the ue_postconstructor. As you can see in Figure 4 the problem is solved because the ue_PostConstructor is invoked after the NVO is resident.

Figure 4: Sequence when CCUO code move to post constructor

Now it looks like the NVO is #1 but in truth it's really not. It's there because we lost the ability to record the CCUO constructor (which is still #1) and we recorded the Window Open (which is really #2) after we created the logging service. So the service is really still #3. It just faked its way to the top of the list. The question still remains: how can we really move the NVO up to the #1 position? In the sections that follow, I'll show you two methods to escalate NVO instantiation.

Solution 2: Auto Instantiate the NVO
In this approach you mark the NVO to be AutoInstantiated on its property sheet. That means THE VARIABLE DECLARATION IS THE INSTANTIATION. Figure 5 shows the design time AutoInstantiate designator.

Figure 5: AutoInstantiate designator

In addition, you must remove the now illegal CREATE statement from the Open Event. Since instance variables are allocated before controls are instantiated, the NVO reference is moved up to #1. Figure 6 shows our NVO in the top position.

Figure 6: The NVO is #1

However, a word of caution is appropriate: autoinstantiated NVOs behave like structures. That is, assignment statements cause a copy of the NVO to be passed, not a pointer to the original object. The will be a problem if your NVO maintains internal state.

Solution 3: Letting the Painter Define the NVO
The window painter has a little-known view called the Non-Visual Object List. As you can see in Figure 7, in the default layout, it's located on a tab on the bottom of the same folder containing the Properties View.

Figure 7: Non-Visual Object List

It lists NonVisual User Objects that you place on the layout surface using the painter. You can both drag and drop a non-visual control from the System Tree onto the Layout or use a menu item to accomplish the deed. Figure 8 shows the Insert Object menu on the Window Painter.

Figure 8: Placing an NVO on the layout

When you place an NVO, the painter gives it a default name just like any visual control - class name prefix followed by a numeric suffix. You can use the property sheet to have the name follow your standard naming convention. Figure 9 shows the view after defining an NVO. Please note that you can only define non-autoinstantate NVOs in this manner.

Figure 9:Non-Visual Object List

The painter is a privileged character. It can write code in framework source code places that are not in the developer domain. In our case, the painter defines the NVO in the Type Declaration section and instantiates it in the CREATE event. That means that the NVO is instantiated earlier in the instantiation process. Figure 10 shows part of the painter-generated code.

Figure 10: Generated Source Code

In addition to rising to number 1 in the pecking order, the NVO garners two other bonus features from this approach; first, the NVO behaves like an NVO. Assignment statements assign the address of a single object in memory; state is retained. Second, like a visual control the NVO becomes an inner control defined within the window. As you can see in Figure 11, you can write additional event handler code for it in the window painter. Now that may prove useful some day!

Figure 11: NVO in the Window Script Editor

One mystery still remains unsolved. A careful read of the CREATE code shows the NVO created AFTER the CCVOs. By all rights and logic, NVO calls in CCUO constructors should fail. But they WORK!!? Obviously there is some sleight of hand going on inside the runtime.

PowerBuilder, like just about everything in life, offers a reasonable face value experience, but provides hints and tools that you can follow and use to rise above the pack. Armed with the desire for a better solution and a bit of fortitude to see it though, you'll find a way to write better code, thereby becoming #1 in the pecking order.

More Stories By Yakov Werde

Yakov Werde, a 25 year IT industry veteran, is a member of TeamSybase and the newly formed Sybase Customer Evangelist Team. Yakov is a recognized author, speaker and trainer who has been designing and delivering PowerBuilder, .NET, EaServer, Web App Development, and Java training for over 14 years to corporate, military and government developers. Prior to discovering his aptitude as an educator, Yakov worked as an architect, project manager and application coder in the trenches of application software development. Yakov holds a Masters in Education with a specialty in instructional design for online learning from Capella University and a BS in math and computer science from Florida International University. Yakov, managing partner of eLearnIT LLC (www.elearnitonline.com), authors and delivers workshops and web based eLearning tutorials to guide professional developers toward PowerBuilder Classic and .NET mastery. Follow Yakov on Twitter as @eLearnPB

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

Most Recent Comments
acartagenas 08/21/11 05:13:00 AM EDT

Hi Yakov,

There's another approach in order to put the NVO on top of CVUO with regards to instantiation.

Instead of explicitly placing the CVUO on the window control, we can set it as instance variable. Thereby instantiating it using CreateUserObject function of the window, thus, you have the luxury of selecting in w/c order you want to instantiate the NVO and CVUO.


Latest Stories
As IoT continues to increase momentum, so does the associated risk. Secure Device Lifecycle Management (DLM) is ranked as one of the most important technology areas of IoT. Driving this trend is the realization that secure support for IoT devices provides companies the ability to deliver high-quality, reliable, secure offerings faster, create new revenue streams, and reduce support costs, all while building a competitive advantage in their markets. In this session, we will use customer use cases...
Digital Transformation: Preparing Cloud & IoT Security for the Age of Artificial Intelligence. As automation and artificial intelligence (AI) power solution development and delivery, many businesses need to build backend cloud capabilities. Well-poised organizations, marketing smart devices with AI and BlockChain capabilities prepare to refine compliance and regulatory capabilities in 2018. Volumes of health, financial, technical and privacy data, along with tightening compliance requirements by...
The IoT Will Grow: In what might be the most obvious prediction of the decade, the IoT will continue to expand next year, with more and more devices coming online every single day. What isn’t so obvious about this prediction: where that growth will occur. The retail, healthcare, and industrial/supply chain industries will likely see the greatest growth. Forrester Research has predicted the IoT will become “the backbone” of customer value as it continues to grow. It is no surprise that retail is ...
Evan Kirstel is an internationally recognized thought leader and social media influencer in IoT (#1 in 2017), Cloud, Data Security (2016), Health Tech (#9 in 2017), Digital Health (#6 in 2016), B2B Marketing (#5 in 2015), AI, Smart Home, Digital (2017), IIoT (#1 in 2017) and Telecom/Wireless/5G. His connections are a "Who's Who" in these technologies, He is in the top 10 most mentioned/re-tweeted by CMOs and CIOs (2016) and have been recently named 5th most influential B2B marketeer in the US. H...
Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settlement products to hedge funds and investment banks. After, he co-founded a revenue cycle management company where he learned about Bitcoin and eventually Ethereal. Andrew's role at ConsenSys Enterprise is a mul...
The best way to leverage your Cloud Expo presence as a sponsor and exhibitor is to plan your news announcements around our events. The press covering Cloud Expo and @ThingsExpo will have access to these releases and will amplify your news announcements. More than two dozen Cloud companies either set deals at our shows or have announced their mergers and acquisitions at Cloud Expo. Product announcements during our show provide your company with the most reach through our targeted audiences.
DevOpsSummit New York 2018, colocated with CloudEXPO | DXWorldEXPO New York 2018 will be held November 11-13, 2018, in New York City. Digital Transformation (DX) is a major focus with the introduction of DXWorldEXPO within the program. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of bus...
With 10 simultaneous tracks, keynotes, general sessions and targeted breakout classes, @CloudEXPO and DXWorldEXPO are two of the most important technology events of the year. Since its launch over eight years ago, @CloudEXPO and DXWorldEXPO have presented a rock star faculty as well as showcased hundreds of sponsors and exhibitors! In this blog post, we provide 7 tips on how, as part of our world-class faculty, you can deliver one of the most popular sessions at our events. But before reading...
DXWorldEXPO LLC announced today that "Miami Blockchain Event by FinTechEXPO" has announced that its Call for Papers is now open. The two-day event will present 20 top Blockchain experts. All speaking inquiries which covers the following information can be submitted by email to [email protected] Financial enterprises in New York City, London, Singapore, and other world financial capitals are embracing a new generation of smart, automated FinTech that eliminates many cumbersome, slow, and expe...
Cloud Expo | DXWorld Expo have announced the conference tracks for Cloud Expo 2018. Cloud Expo will be held June 5-7, 2018, at the Javits Center in New York City, and November 6-8, 2018, at the Santa Clara Convention Center, Santa Clara, CA. Digital Transformation (DX) is a major focus with the introduction of DX Expo within the program. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive ov...
DXWordEXPO New York 2018, colocated with CloudEXPO New York 2018 will be held November 11-13, 2018, in New York City and will bring together Cloud Computing, FinTech and Blockchain, Digital Transformation, Big Data, Internet of Things, DevOps, AI, Machine Learning and WebRTC to one location.
DXWorldEXPO LLC announced today that ICOHOLDER named "Media Sponsor" of Miami Blockchain Event by FinTechEXPO. ICOHOLDER give you detailed information and help the community to invest in the trusty projects. Miami Blockchain Event by FinTechEXPO has opened its Call for Papers. The two-day event will present 20 top Blockchain experts. All speaking inquiries which covers the following information can be submitted by email to [email protected] Miami Blockchain Event by FinTechEXPO also offers s...
DXWorldEXPO | CloudEXPO are the world's most influential, independent events where Cloud Computing was coined and where technology buyers and vendors meet to experience and discuss the big picture of Digital Transformation and all of the strategies, tactics, and tools they need to realize their goals. Sponsors of DXWorldEXPO | CloudEXPO benefit from unmatched branding, profile building and lead generation opportunities.
@DevOpsSummit New York 2018, colocated with CloudEXPO | DXWorldEXPO New York 2018 will be held November 11-13, 2018, in New York City. From showcase success stories from early adopters and web-scale businesses, DevOps is expanding to organizations of all sizes, including the world's largest enterprises - and delivering real results.
Dion Hinchcliffe is an internationally recognized digital expert, bestselling book author, frequent keynote speaker, analyst, futurist, and transformation expert based in Washington, DC. He is currently Chief Strategy Officer at the industry-leading digital strategy and online community solutions firm, 7Summits.