Global Variables - Confused!

Hey all,
Just started with Xojo (VB6 Programmer here) and global variables there are very straightforward. I’m thinking of starting to move my games to Xojo and since certain stats need to be accessed globally, I started tinkering a bit.

I created a Module, opened a Property on it, and entered these lines into it:

life as Integer gold as Double pnam as String

They are all assigned as Global on the dropdown on the side.

Now on the main form I have a label (called Label1) that I have in the Activate (I assume this is equivalent to Form_Load on VB?) action these lines:

life=20 Label1 = life

I get a ‘life’ doesn’t exist error. Huh? Did I do something wrong?

[quote=77312:@Derek DiBenedetto]life=20
Label1.Text = Str(life)[/quote]

Label1.text = str(life) 

EDIT: Eli beat me to it…

Got it, but its saying the variable ‘life’ does not exist at all, and I declared it (I think) properly. Step by Step, how would I declare the variable called life so it can be accessible to all forms as a global variable?

  1. Add a module to your application.
  2. Right click your module, and ‘add to…’ a property named ‘life’.
  3. Set the ‘life’ property to an ‘integer’ type and set the scope to ‘Global’.

That should be it.

I think I see your confusion. You typed that text into the property where the code editor normally is, which is not correct. You actually set the attributes to that specific property using the ‘inspector’ tab to the right of the screen.

Got it…so for more variables, just create a new property for each one?

And the way to display numbers in a label is always the str(variable) convention?

Yes, you need to add a new variable each time, you can’t type all the declarations together. To make it easier you can use ctrl+P (if I am not wrong, I don’t have the IDE in front of me right now so I can’t try) to create a new variable instead of right click + choosing " add property".

Yes, Xojo is not doing any type conversion for you, so you have to pass all the methods the type of variable they expect. It’s a “strongly typed language”.


Actually, CStr is better for displaying numbers–it respects the user’s settings for things like the decimal/thousands marks, while Str forces the US conventions. (Str is better for storing in a file where the expected user is a program instead of a human.)