BlitzMax's use of the Global Variable?

BlitzMax Forums/BlitzMax Programming/BlitzMax's use of the Global Variable?

JoJo(Posted 2005) [#1]
Does anyone know what's mark's intention is on the use of Global Variables in Max?

Global strName:String = "Global variable outside the type"
Local s:TName = TName.Create()
Print strName
s.display


Type TName
	Global strName:String = "Global variable inside type"
	
	Function Create:TName()
		Local t:TName = New TName
		Return t
	End function

	Method Display()
		Print strName
	End Method
End Type

I don't know if this is something that will be fixed later or if this is intentional.

I have a global variable outside the type, and one declared inside the type with the same name; and they are treated as two different variables.

Does anyone have any answers on this?
I mean I can get used to this if its meant to be like this, but hmmm....


bradford6(Posted 2005) [#2]
as I understand it, global namespace and Type(class) namespace are different.

if you declare a program global outside of a type, it can be used inside the type

if you declare a program global inside a type, it will override the program global

Global strName:String = "Global variable outside the type" 
Print strName		'Global
Local T:TName = New TName
Local Z:ZName = New ZName
Local Foo:Foo_Name = New Foo_Name

T.show()
z.show()
Foo.show()

Type TName 
	Global strName:String = "Global variable inside type T" 
	Method show()
		Print strName
	End Method	
End Type 
Type ZName 
	Global strName:String = "Global variable inside type Z" 
	Method show()
		Print strName
	End Method	
End Type 
Type Foo_Name Extends ZName
	Global strName:String = "Global variable inside extended type FOO"
	Method show()
		Print strName
	End Method	

End Type






JoJo(Posted 2005) [#3]
Ok, understand now. It's just threw me for a loop at first.

It also makes a variable local to that block of code.
	Local a:Int = 2

	If a = 2 Then
		Global b:Int
		b=8
	EndIf
	Print b


b is 8 inside the if block, but 0 once you leave the if stmt as someone told me that before.

We are talking about two different b variables.

I like this because its like this in vb.net.


AaronK(Posted 2005) [#4]
Global inside a function or a type works like 'static' in C++. Try this.

test()
test()

Function test()
Global b = 1
Print b
b = b +1
End Function

It prints out
1
2

So the first time it's used it's set to 1 and from them on each time in test, it's incremented


Aaron


Robert(Posted 2005) [#5]
Globals vars inside a type 'belong' to that type instead of belonging to the program.

To access it you use TypeName.globalName

I believe that this is both intentional and desireable.


BlitzSupport(Posted 2005) [#6]
It is intentional -- it lets all objects of the same type share a variable. Good for counting objects, wrapping a single TList for all objects into the type definition, etc.


ImaginaryHuman(Posted 2005) [#7]
Hmm this is cool, and probably explains why when I created a global within an if and a for-next the value dissapeared later.


JoJo(Posted 2005) [#8]

It is intentional -- it lets all objects of the same type share a variable. Good for counting objects, wrapping a single TList for all objects into the type definition, etc.



That's cool.


AaronK(Posted 2005) [#9]
Correct. I think it is causing confusion cause it shouldn't be named Global, when, well it's not, It's local (Except for the "real" Global) And more than that it's value remains each time through the local code block.

Maybe a name change is in order Mark?

Aaron


tonyg(Posted 2005) [#10]
I understand having type globals vs 'standard' globals.
However, are global variables redundant within for/next, IF and standard functions then? The value isn't kept within a function (presumably because you have to redeclare it each time).
If you have a nested for/next and declare the variable as global in the outer loop than it can be used within the inner but not vice-versa.
All very confusing.


Hotcakes(Posted 2005) [#11]
tonyg, sure you have to declare Globals inside a function to use them that way, but you don't have to assign them a number

Global var

If during the course of that function var=15 (the first time the function is called it will be 0 of course) then the next time the function is called it will start at the value of 15.

Very handy for many things, such as if a handle is used to load an image inside a function, you don't want that image reloaded every time the function is called...


tonyg(Posted 2005) [#12]
Yep, sorry. That was me being a muppet.
The test I ran didn't give the global a chance to change.


Hotcakes(Posted 2005) [#13]
Lol that's slightly funny. =]