Page 1 of 1
[Implemented] STATIC
Posted: Sat Jul 13, 2002 9:07 am
by BackupUser
Restored from previous forum. Originally posted by Danilo.
STATIC for use in procedures.
Like "protected" (rename that to LOCAL plz ),
but it doesnt loose its value.
LOCAL and STATIC are available in all BASICs,
but not in PB...
cya,
...Danilo
(registered PureBasic user)
Posted: Sat Jul 13, 2002 10:08 am
by BackupUser
Restored from previous forum. Originally posted by fweil.
I noticed that static variables are not provided in PB, but in the meanwhile (until it exists !) I use to set global variables instead.
This is not 'academic' but works. Just the code is less easy to read / understand.
Anyway static variables are useful.
...
Francois Weil
14, rue Douer
F64100 Bayonne
Posted: Sat Jul 13, 2002 10:57 am
by BackupUser
Restored from previous forum. Originally posted by PB.
> LOCAL and STATIC are available in all BASICs, but not in PB...
Huh? Any non-global variable inside a procedure is a local variable to that
procedure only, so no need for a "Local" keyword...
PB - Registered PureBasic Coder
Posted: Sat Jul 13, 2002 1:31 pm
by BackupUser
Restored from previous forum. Originally posted by fweil.
I agree ...
I did'nt pointed out for Local which is native but only Static (to maintain a variable value when leaving a procedure to have it left unchanged when calling again).
Francois Weil
14, rue Douer
F64100 Bayonne
Posted: Sat Jul 13, 2002 9:14 pm
by BackupUser
Restored from previous forum. Originally posted by Danilo.
> Huh? Any non-global variable inside a procedure is a local
> variable to that procedure only, so no need for a "Local" keyword...
Its already implemented in PureBasic,
but with the name "protected".
Code: Select all
Global a
Procedure test()
Protected a ; LOCAL!
MessageRequester("INFO","Local a = "+Str(a),0)
EndProcedure
test()
a = 12
test()
So only STATIC is missing...
cya,
...Danilo
(registered PureBasic user)
Posted: Sat Jul 13, 2002 9:57 pm
by BackupUser
Restored from previous forum. Originally posted by PB.
Code: Select all
Global a
Procedure test()
Protected a ; LOCAL!
MessageRequester("INFO","Local a = "+Str(a),0)
EndProcedure
test()
a = 12
test()
Of course "a" is going to change, because you made it global!
If you wanted to use a local variable inside a procedure then why give it the
same name as a global variable in the first place?
PB - Registered PureBasic Coder
Edited by - PB on 13 July 2002 23:18:37
Posted: Sat Jul 13, 2002 11:25 pm
by BackupUser
Restored from previous forum. Originally posted by Danilo.
@
PB:
Do you know what the keyword "PROTECTED" in my example is ??
There is a reason Fred has added this "LOCAL".
You can use all variable names you want
in your procedure, even if the name is
global - for this procedure its LOCAL.
Code: Select all
Global myvariable
Procedure test1()
Protected myvariable ; LOCAL!
myvariable = 123
EndProcedure
Procedure test2()
myvariable = 123
EndProcedure
test1()
MessageRequester("INFO","value of variable = "+Str(myvariable),0)
Test2()
MessageRequester("INFO","value of variable = "+Str(myvariable),0)
Test it!
Maybe you see the difference now.
Its very useful if you have lot of procedure
and if you import procedures from other people.
Anyway... STATIC is requested here.
cya,
...Danilo
(registered PureBasic user)
Posted: Sat Jul 13, 2002 11:43 pm
by BackupUser
Restored from previous forum. Originally posted by PB.
> Do you know what the keyword "PROTECTED" in my example is ??
Yes, it stops the global variable "myvariable" being used in the first procedure.
> Maybe you see the difference now.
Well... I see that the second procedure is using the global variable again, as
it should, because it hasn't been protected. I know what you mean, and why you
are requesting it, and I agree that it would be useful, especially for your
comment of "its very useful if you import procedures from other people".
PB - Registered PureBasic Coder
Posted: Sun Jul 14, 2002 7:42 am
by BackupUser
Restored from previous forum. Originally posted by horst.
So only STATIC is missing...
Why not use SHARED ??
Horst
Posted: Sun Jul 14, 2002 8:22 am
by BackupUser
Restored from previous forum. Originally posted by Danilo.
Code: Select all
GLOBAL = can be used anywhere in the source
SHARED = for procedures
gives access to a variable in the main program
LOCAL = for procedures
use a LOCAL variable that is only used inside
this procedure. This variable is 0 everytime
the procedure starts again.
LOCAL has higher priority than GLOBAL (very important!).
In PureBasic its called "protected", not like ALL other compilers...
STATIC = for procedures
same as LOCAL, but doesnt loose its contents.
STATIC has higher priority than GLOBAL.
I dont know the reason you guys dont understand that.
Its available in all BASIC languages for some reason.
(But its not implemented right in many languages)
Yes, i´m using SHARED for my STATIC variables atm.
But thats not the right way if you deal with many, many
procedures - because LOCAL ( <-- read that word please )
is used local only and doesnt access GLOBAL variables
in the main program.
Same for STATIC, because it is like LOCAL without
loosing its contents.
Whats wrong if you write a big program with 374 procedures
and 78.000 lines of code ??
Maybe you loose overview about all variables sometimes.
With LOCAL and STATIC you can use whatever variable names
you want in ALL procedures WITHOUT CONFLICTING with the
main program.
Maybe you make all variables GLOBAL and declare
it as
Code: Select all
myvariable1
[...]
myvariable120
[...]
myvariable295
Nice coding style, eh ??
I dont like that - and million other coders dont like it too..
...thats the reason there is something like LOCAL and STATIC
available in every good language.
cya,
...Danilo
(registered PureBasic user)
Posted: Sun Jul 14, 2002 10:33 am
by BackupUser
Restored from previous forum. Originally posted by PB.
> I dont know the reason you guys dont understand that.
I do understand. I just didn't think it was a necessary feature, but your
comment about importing code from other sources changed my mind, and I now
agree with you.
> Whats wrong if you write a big program with 374 procedures
> and 78.000 lines of code ??
> Maybe you loose overview about all variables sometimes.
I never lose my overview of my apps.
PB - Registered PureBasic Coder
Posted: Sun Jul 14, 2002 10:53 am
by BackupUser
Restored from previous forum. Originally posted by Danilo.
> I never lose my overview of my apps.
Thats why i want LOCAL and STATIC - it makes this things
much easier to read.
If i read procedure 212 in include file 15 i can see the
LOCAL and STATIC variables directly in the procedure
without goin back to the mainfile and search the variable
between the 1435 GLOBAL variables.
It can happen easily that somebody has the variable "timer_count"
in procedure 302 and 8 month later he is using it in the main-app
as global.
So the SHARED and GLOBAL variables collide and somewhere
in the big source is a little error you search for hours.
For the moment i have all the overview too, but i´d like
this stuff for future use when i reach the 78.000 lines limit...
BTW: This was also a request in the german forum
some month ago. All people said that this would be
very useful.
I write it here to remember Fred about it...
...hope he doesnt forget it.
cya,
...Danilo
(registered PureBasic user)