Variables and arrays not available to pushbutton

I’m obviously missing something, as a beginner in Xojo!

My app opens a text file in which data is stored in 3 lines for each complete record. The code creates 3 arrays with the data, and also displays this in 3 Listboxes. The arrays are created thus:

Var aTitle(), aMP3(), aPDF() as String ’ 3 Arrays for the data

All this works as it should. The code for this is all in Controls / Listbox1 / Open. There is no code in the other 2 Listboxes

I have put a PushButton below the displayed Listboxes to do further processing when clicked. At its simplest, this will just be a MessageBox displaying the last entry in all the arrays.

The counter variable for the arrays is i and the code is

MessageBox("Last Record: " + aTitle(i) + EndOfLine + EndOfLine + aMP3(i) + EndOfLine + EndOfLine + aPDF(i))

This MessageBox code works if it is in the Listbox1 / Open code

Here is the problem. The MessageBox code does not work if I move it to the Pushbutton1 / Action code because the arrays and other variables are not available in the Pushbutton1 / Action code (“Does not exist” errors).

I’ve seen a similar question on the forum where a solution given was " Make the array a “more global” property (property of the window or app or a module) and access it from both places." The OP in response said “Just dropped a property on the window and now I can access from everywhere” but I don’t understand how I would do that, though it sound to be just what I need!

Please tell me how I get the arrays and variables in the Listbox1 / Open code to be available in PushButton1 / Action


Var / Dim is used to create local variables which means they won’t exist outside of the method.

The solution is to create these as properties. See the Xojo docs for more information.

As @kevin_g said you should move your variables to “properties” of the Window (or the App) if you want to use them more “globally”. As an example, for a Window, you can select the Window on the navigator and then click on the Add button (the button with a “+”) and select “Property”. Or you can right click on the window and select “Add to Window” and select “Property”. Once you add the property it becomes usable to anything within the scope of that Window by merely refencing it by name.

Thanks so much for these replies. They have pointed me in the right direction and I’ve now got the variables and arrays in Properties so other parts of the code can use them. Section 5.7 of Introduction to Programming with Xojo was also helpful for this.
Easy when you know how!!