Storage of global variables

I’m converting a rather large Visual Basic project to Xojo. In VB I have a module where all of my global declarations are made. There are about 230 lines. Is there a corresponding space in Xojo where such variable declarations can be placed? I’ve read the section on MODULES in the “Introduction to Programming” about storage of globals, but it’s left me baffled.

Create a property within a Module and set its scope to Global or Protected.

Let’s say you call the module Globals and your property MyProperty. If it’s scope is set to Global, then anywhere in the code you can refer to it as MyProperty. If you set the scope to Protected, you can still use that property anywhere, but must use the entire namespace as Globals.MyProperty. This is generally preferred to prevent conflicts with other property and variable names.

Okay, I have gotten Xojo to accept my first few globals by creating individual properties. But the code still doesn’t run. My app uses a large array of data samples, and I’m trying to use a Bevel Button on the main screen to erase the global array with the following code:

dim x as integer

for x = 0 to sample_limit
pvals(x) = 0

The error is “OutOfBoundsException” when it tries to set pvals(0) = 0.

I defined a Property of “pvals()”, with a Type of “Double”, and a Scope of “Global”


Redim pvals(sample_limit)
Redim pavgs(sample_limit)

Before the loop, or…

Pvals.append 0
Pavgs.append 0

As the setters.

The way you’ve declared pvals, it’s an empty array, which means there is no element 0 (the first element). To use it, you must first add elements to it, and, as Greg mentioned, you can use Redim to do that, or you can declare it with the right size in your declaration: pvals(20) As Double.

Later, if you want to erase the array, you don’t have to set every element to 0 (which should really be 0.0 anyway because it’s a double), you can simply redim it to an empty array, then back to the size you want.

redim pvals( -1 ) // Empty array
redim pvals( sample_limit ) // All initialized to 0.0

Thanks to you both for your help. My code works with the redim, but I see now that I can also define my Property as “pals(90000)”.


BTW, don’t get overly attached to modules. They are good for some things, but are often not the best solutions. Instead, consider creating classes that encapsulate your data and even perform automatic functions on it through methods and computed properties.