Multi Attribute, Global Array Variables (from VB6)

Hi all, does Xojo have this sort of global variable assigning?
Type, End Type, & Global “” As “”?

Example in VB6 Code:

Type Unit_Type
Name As String 'Name of unit
Icon As Integer 'Icon of unit
List_Owned(50) As Boolean 'All the lists owned by the unit
End Type
Global Super_Unit(300) As Unit_Type 'Collection of AI bots

Is there a global array, assigning, equivalent in Xojo? The above is from a VB6 module.
I’m having trouble finding stuff in the forum in regards to this as the ‘Type’ function refers to different things.
Before anyone screeeees ‘global variable! away array! away!’, don’t stress. I know what I’m doing. :japanese_ogre:
Thanks for your time.

Hello,

You can use a Class for that. Add a new class to your project and give it all the properties you need.

Then you can declare arrays of your class elements.

HTH,

Julen

EDIT: Let us know if you need help implementing this, since it involves a couple more steps than I have mentioned

Create a module and call it something meaningful like “Globals”. Add properties to that module and set their scope to Global or Protected. Those will be available to you everywhere.

If you create a property called MyArray() as String, then set it to Global, you can access “MyArray” in your code. If you set it to Protected, you can access it as “Globals.MyArray” in your code. That is preferred.

Good Morning all,
Thank you Kem and Julen, I’ll give those ideas a go. :grinning:

Kem and Julen,
I’ve had a close look at setting variables in the right hand text boxes. What is weird and making this harder for me is that Xojo calls VB Subroutines/Routines/Functions “methods”, and VB Variables are named “properties”.

Julen, I’m not sure about Classes (is that what is called the VB ‘Type’ function? I added one in, then added ‘Properties’ under it and can then write variables there. I assume that’s what you mean.

Though, what happens if I have to make multiple Types (classes) which use identical variable lists? (properties)
From my first post, I mean like adding in this in VB
Global Super_Unit(300) as Unit_Type
Global Military_Unit(300) as Unit_Type
Global Civilian_Unit(300) as Unit_Type

Unit_Type is a class, which has properties Name, Icon, List_Owned. Super_Unit, Military_Unit and Civilian_Unit will all be properties of your Module and will be declared almost identically as VB, except that the IDE separates it out into separate fields.

Eg., go to the module and add a property. Set the fields as
Name: Super_Unit(300)
Type: Unit_Type
Scope: Global

Do the same for the other 2 arrays.

Something that hasn’t come up yet is that you will have to instantiate instances of the class before you can use it. This could be something as simple as putting this code in the open event of your window.

for i as integer = 0 to 299
   Super_Unit(i) = new Unit_Type
next

I tend to instantiate objects as needed, rather than preallocate them. You can also build the array as needed with Super_Unit.Append(new Unit_type). That allows you to have a variable sized array instead of a fixed 300 slots.

1 Like

Thanks Tim, looks good to me!
I guess I just have to get used to the way Xojo handles modules and declarations now. I personally prefer all code in one window, not the text box entry points. But we’ll get there. Thanks for the help! :grinning:

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.