1. Friedrich Bräuning
  2. PowerBuilder
  3. Wednesday, 29 September 2021 11:32 AM UTC

Hello,

we have the following Problem in PB2019 and PB2021:

We have a function which dynamically returns us a nonvisual Object based on a string.

Choose Case as_Type
   Case "attrib_1"
      attrib_1 lnv_Attrib1
      lnv_return = lnv_Attrib1
   Case "attrib_2"
      attrib_2 lnv_Attrib2
      lnv_return = lnv_Attrib2
   Case ...
End Choose

These are all AutoInstantiated, which means we can't just use "create using as_Type"
The Problem is, that when we call this function with as_Type = "attrib_2", Powerbuilder creates all NVOs which are autoinstantiated.

Powerbuilder even has this problem when it will never execute the code:

If (1=2) Then
   attrib_1 lnv_Attrib1 //attrib_1 is autoinstantiate
End If

Here the constructor of attrib_1 is called, which is just plain false, as Powerbuilder never executes the code of "attrib_1 lnv_Attrib1".

Is there any workaround for this PB-behaviour ?
Is this the intended behaviour of Powerbuilder?

Thanks for your help.

 

John Fauss Accepted Answer Pending Moderation
  1. Wednesday, 29 September 2021 15:17 PM UTC
  2. PowerBuilder
  3. # 1

Hi, Friedrich - 

Are you thinking that PB "executes" an object's declaration at the point in a script where it appears? That's what it sounds like you're saying.

If so, that's not the way it works. Variable/structure/auto-instantiated object declarations are simply information for the compiler as to how much memory to allocate and initialize. It matters not where in the script the declaration is located, as long as a declaration precedes any use of the variable/structure/object that is being declared.

An auto-instantiated non-visual object behaves similar to a structure or standard datatype except the NVO also contains methods.

This is all by design. If you want the creation of NVO's to behave differently you'll have to manually instantiate/create them and manage this yourself.

Best regards, John

 

Comment
There are no comments made yet.
René Ullrich Accepted Answer Pending Moderation
  1. Wednesday, 29 September 2021 12:27 PM UTC
  2. PowerBuilder
  3. # 2

Hi Friedrich,

I think this is intended behaviour. Variable declaration in a script is independend from control.

You can check this:

if 1 = 2 then
	int i
end if
i = 1
messageBox ("", i)

So in your case it instantiates all autoinstantiated objects.

 

Workaround:

Write a function or event to create each of your types.

Choose Case as_Type
   Case "attrib_1"
      lnv_return = of_create_attrib_1()
   Case "attrib_2"
      lnv_return = of_create_attrib_2()
   Case ...
End Choose

function attrib_1 of_create_attrib_1()
   attrib_1 lnv_Attrib
   return lnv_Attrib

function attrib_2 of_create_attrib_2()
   attrib_2 lnv_Attrib
   return lnv_Attrib

...

 

 

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.
We use cookies which are necessary for the proper functioning of our websites. We also use cookies to analyze our traffic, improve your experience and provide social media features. If you continue to use this site, you consent to our use of cookies.