1. Michael Connell
  2. PowerBuilder
  3. Friday, 5 November 2021 11:07 AM UTC

Hi,

We have inherited a legacy PB application and whilst going through the code recently I noticed the following (in PFC of all things):

if IsValid (gnv_app) then
	if IsValid(gnv_app.inv_debug) then
		if IsValid (gnv_app.inv_debug.inv_sqlspy) then
			<some code here>
		end if 
	end if
end if

But isn't this the same as:

if IsValid (gnv_app.inv_debug.inv_sqlspy) then
	<some code here>
end if 

My logic being that if inv_sqlspy is valid then gnv_app.inv_debug and gnv_app must also be valid. Or is there some coding subtlety I'm not getting here? 

Apologies if this comes across as a dumb question.

Regards,

Michael Connell.

Michael Connell Accepted Answer Pending Moderation
  1. Tuesday, 16 November 2021 08:36 AM UTC
  2. PowerBuilder
  3. # 1

John,

Thank you very much for your reply.

That was exactly the sort of thing I was suspicious of.

Regards,

Michael Connell.

Comment
There are no comments made yet.
John Fauss Accepted Answer Pending Moderation
  1. Friday, 5 November 2021 14:16 PM UTC
  2. PowerBuilder
  3. # 2

Hi, Michael - 

Your alternative code only works the same as the original when the app manager object (gnv_app) and the Debug service object (inv_debug) are instantiated. In a service-based architecture like the PFC, the objects implementing the various services and features/sub-services will only exist when they are enabled, so the original code snippet you posted is not only quite common within the PFC, it is more importantly, correct.

In a PFC application, it is generally assumed that the gnv_app object will exist, but it only has to exist if the application is utilizing PFC services and objects... it is not a requirement. The Debug service object (the inv_debug object reference instance variable of gnv_app) exists only when that service is enabled, and the SQL Spy sub-service of the Debug service  (the inv_sqlspy object reference instance variable of inv_debug) likewise.

The alternative code you've supplied executes successfully only when gnv_app and inv_debug are both valid. If executed, resolving the reference to the inv_sqlspy object will produce a runtime error when either or both of the gnv_app or inv_debug objects are not valid. The original code works under all conditions.

As a test, I placed the following code in a command button in a window of a PFC application where gnv_app is valid and the Debug service is not enabled (i.e., is not valid):

String ls_msg

If IsValid(gnv_app.inv_debug.inv_sqlspy) Then
   ls_msg = "IsValid results: True"
Else
   ls_msg = "IsValid results: False"
End If

MessageBox("Test Results",ls_msg)

When executing, a Null Object Reference error occurs at line 3 (the IF-statement) because inv_debug is not valid.

HTH
John

Comment
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Friday, 5 November 2021 12:52 PM UTC
  2. PowerBuilder
  3. # 3

Hi Michael;

   Yes, a bit of code overkill. It would make sense for the three IF's if there were further embedded code in each IF code block - say with an ELSE thrown in as well.

Regards ... Chris

Comment
  1. René Ullrich
  2. Tuesday, 16 November 2021 09:45 AM UTC
I think, your are not correct. If gnv_app is not valid then IsValid(gnv_app.inv_debug.inv_sqlspy) will throw an Null pointer exception. It checks if inv_sqslspy is valid. It doesn't check the whole expression.
  1. Helpful
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.