Static variables

BlitzMax Forums/BlitzMax Beginners Area/Static variables

Ant(Posted 2006) [#1]
Does Blitmax support static variables?
thanks


tonyg(Posted 2006) [#2]
Depends how you are using the term 'static variables'.
You can use Constant variables, function variables and 'type/class' variables. Each could be considered static variables.


Ant(Posted 2006) [#3]
Sorry I should have been more specific - I meant local variables specifically?


tonyg(Posted 2006) [#4]
That's confused me even more now.
You can use local variables. Check under Language / Variables.
How do you want to use these variables?
You might want to check the Beginner's Guide to Bmax in the tutorials section.


Yan(Posted 2006) [#5]
Does BMax have static variables?


bradford6(Posted 2006) [#6]
I think what you need to focus on at this point is the variables 'scope' (i.e where the variable can be accessed)

a GLOBAL and CONSTANT can be accessed anywhere
a Local can be accessed only within it's scope (Function, Type, Method etc)


Ant(Posted 2006) [#7]
Apologies, I'm getting tangled up in my own confusion. I meant - is there any way to declare a local variable as static (that is next time the variable is encountered its previous value is stored) - but I guess the answer i no and the solution to this is in tonyg's first response. Apologies for the confusion


Yan(Posted 2006) [#8]
Blimey...

THIS IS A LINK >>> Does BMax have static variables? <<< THIS IS A LINK

I'm not going to post it again. ;o)


Jay Kyburz(Posted 2006) [#9]
Melvin, your first link just looks like you are asking the question again.


FlameDuck(Posted 2006) [#10]
I meant - is there any way to declare a local variable as static (that is next time the variable is encountered its previous value is stored) - but I guess the answer i no and the solution to this is in tonyg's first response.
The answer is actually yes (as Melvin points out).
Function staticTest()
	Global myVar:Int
	
	myVar:+1
	Print myVar
EndFunction

staticTest
staticTest
staticTest
staticTest
staticTest
staticTest



Russell(Posted 2006) [#11]
FlameDuck's example DOES preserve the value of the variable upon further calls (because it's global), so in that respect it is static. But if Ant is refering to local variables with the same characteristics, then no.

I'm surprised, actually, that BM allows the declaration of a global variable within a function like that.

Russell


FlameDuck(Posted 2006) [#12]
I'm surprised, actually, that BM allows the declaration of a global variable within a function like that.
It doesn't have global scope, thus is not a global. Try it, it works. So does this:
SuperStrict

Graphics 32,32

Local terminate:Int = False
While Not terminate
	WaitEvent
	If EventID() = EVENT_KEYDOWN
		Global myVar:Int
		Select EventData()
			Case KEY_ESCAPE
				terminate = True
			Case KEY_SPACE
				myVar:+1
				Print myVar
		EndSelect 
	EndIf
'	Print myVar ' Enable this, and the program won't compile.
EndWhile



Yan(Posted 2006) [#13]
LOL @ this thread...It's not rocket science! |oD

Print Count()
Print Count()
Print Count()

Print i

Print Count()
Print Count()

End

Function Count()
  Global i
  
  i :+ 1
       
  Return i
End Function