Problems using CheckMenu

BlitzMax Forums/BlitzMax Beginners Area/Problems using CheckMenu

Marco A G Pinto(Posted 2012) [#1]
Hello!

I have been trying to use CheckMenu from inside a function to update (check/uncheck) menu options and it says:
"Unhandled Exception:Attempt to acess field or method of Null object"

I have the menus created in a function and the check command works okay there.

But, when I leave the function and return to the main loop, in the part where it checks if one clicked in the menus, I call another function to update the menu and it shows that error.



Thanks for your help!

Kind regards,
>Marco A.G.Pinto
--------------------


Jesse(Posted 2012) [#2]
globals created inside functions are just static local variables and are only accessible on the function they are created.
You need to create the globals on the main level of your app if you want to have full access to them trough out the whole program.


Marco A G Pinto(Posted 2012) [#3]
@Jesse:

globals created inside functions are just static local variables and are only accessible on the function they are created.
You need to create the globals on the main level of your app if you want to have full access to them trough out the whole program.



Dear Jesse,

Thanks for your reply.

Can't this be reported as a Blitz Max bug? Or be a suggestion to improve in the next version of it? If the variables are global they should work everywhere even if created inside of functions.

The same happens with labels, they should also work inside functions but do not.

I am trying to make the code modular but this makes things not to go very well.

Kind regards,
>Marco A.G.Pinto
-------------------


Jesse(Posted 2012) [#4]
I don't agree as global variables in BMax have different scopes depending on use:

. In the main level of the application the global is accessible throughout the program.

. In a "Type" its accessible directly only throughout the type scope

. And in a function its only accessible through the function scope.


suggestion: The best way to make it modular is to use types( object oriented).


since Bmax is both procedural and object oriented, global serves its functionality best as is.


Note: excessive use of globals is highly discouraged and is a well practice rule for seasoned programmers. Use only what you have to.


Zeke(Posted 2012) [#5]
Global a=1

Function test()
	Global a
	a:+1
	Print "test:a="+a
End Function

test
test
Print "GLOBAL A="+a
test
a=2
test
Print "GLOBAL A="+a

global inside function test
and Jesse said how globals works.