"Protected" inside "If"

Just starting out? Need help? Post your questions and find answers here.
AZJIO
Addict
Addict
Posts: 1312
Joined: Sun May 14, 2017 1:48 am

"Protected" inside "If"

Post by AZJIO »

I saw that some use "Protected" inside "If". I got the impression that it is possible not to declare variables if they are not needed by condition. The compiler generated an error, but this still does not give an unambiguous understanding that the variable will still be declared regardless of the condition. The compiler works line by line, but it will still declare the variable as if it were at the beginning of the function?

Code: Select all

Global fff = 1
Global qqq = 0

Procedure e()
	Protected fff = 0
	If qqq
		Protected fff = 2
	EndIf
EndProcedure

e()
Debug fff
Will these functions produce the same code?

Code: Select all

Procedure e()
	Protected fff = 0
	If qqq
		fff = 2
	EndIf
EndProcedure

Procedure e()
	If qqq
		Protected fff = 2
	EndIf
EndProcedure
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: "Protected" inside "If"

Post by STARGÅTE »

On compiler level the variables are always declared, in terms of that they do not create a systax error, regardless of whether they are located. This is because on compiler level the If is not evaluated and all Protected are compiled.
On the other hand, on evaluation level, a protected variable in an If-statement is not initiallized. This means, an e.g. array is not allocated and ArraySize() will return -1:

Code: Select all

Procedure Test(Value.i)
	If Value = 1
		Protected Dim MyArray.i(10)
	EndIf
	Debug ArraySize(MyArray())
EndProcedure


Test(1) ; Dim MyArray.i(10) is evaluated, ArraySize() = 10
Test(0) ; Dim MyArray.i(10) is not evaluated, ArraySize() = -1
To conclude, there could be a situation, where you do not want to run the whole initialization for an array or a list, but you need to declare the variable, then you can use Protected inside an If.

Edit: There was a discussion from my side here: Protected variables always initialized? Bug or feature in If-statement
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: "Protected" inside "If"

Post by jacdelad »

In short: If the condition is true the variable would be declared twice, which would be an error. So having multiple lines to declare a variable within a procedure is seen as an error, even if if would work, like using if-else and always using just one of the protected.
But, it works well if the procedure can perform a complex or less complex task. If the complex task is within an if-statement you can declare the variables, that are otherwise not needed, right after the if. Want an example?
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
AZJIO
Addict
Addict
Posts: 1312
Joined: Sun May 14, 2017 1:48 am

Re: "Protected" inside "If"

Post by AZJIO »

STARGÅTE got me right. The task was that if the variable is declared dynamically, then there is an opportunity not to spend time initializing the variable so that the function runs faster.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: "Protected" inside "If"

Post by mk-soft »

If the function is not threaded or recursive, then you can use 'Static'. With protected, the memory is always initialised on the stack.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: "Protected" inside "If"

Post by Olli »

I doubt about it : the coder looses the control of its datas after the array allocating condition, if the variables of this condition have changed.

I would say, as with the dynamical arrays, start with a one-cell array (Dim x.i(0) ) and redim this array in the condition :

Code: Select all

If cc
   Redim x.i(lastValue)
EndIf
In a complex procedure, no initial Dim divides this procedure in two areas : the first one forbides the coder to use the array, during the second one is allows him to handle the array...

However, it seems that Protected is not required for arrays. :: I tested it and... yes, protected is required... I prefer handle dynamic arrays.

@azjio

You can use SetWindowData(window, *classStruc) in the initial step,

and after :

Code: Select all

Define *this.class = GetWindowData(EventWindow() )
With *this
 Debug /x
 Debug /y
 Debug /width
 Debug /height
EndWith
in your callback.

It is far simpler. The array is dynamically allocated :

Code: Select all

Structure class
   x.i
   y.i
   width.i
   height.i
   Array myArray.i(0)
EndStructure
Post Reply