1

Any tips on creating a global array to use in a realBASIC project using 'add property'? Pictured below is my attempt.

http://i17.photobucket.com/albums/b52/orubap/basic.jpg (edit: dead link)

Using camModel(1) compiles and runs but it doesn't return anything. Using camModel(4) throws an out of bounds error so I'm guessing I'm half way there.

Suggestions?

Sicco
  • 6,167
  • 5
  • 45
  • 61
jason
  • 13
  • 3

2 Answers2

2

Even though the code compiles, that isn't a valid way of initializing an array. At the very least doing so isn't mentioned anywhere in the manuals. I would say that the compiler is quietly failing on that one, as opposed to flagging it as an error. You'll have to place the values via an init method, say in App.Open. Also, don't forget that array indexes are 0-based, even during initialization. So, going by the code you've given declare the array property for three values:

camModel(2) as String

and then in the App.Open event:

camModel(0) = "Nikon"
camModel(1) = "Sony"
camModel(2) = "Philips"

However, if it were me doing it, I would declare the property thus:

camModel(-1) as String

and then populate with the Array function:

camModel = Array("Nikon", "Sony", "Philips")

That way you can add more models later and not have to futz with the bounds of the array each time.

Philip Regan
  • 5,005
  • 2
  • 25
  • 39
1

If you want to access a global variable using the "Add Property" function, just create a new Module. You can then add a property to the module that can be accessed from anywhere.

To keep your namespace cleaner, you might want to restrict access to the property. Global will allow you to access the property by just using YourVariableName, but you can also change the permissions to protected (the yellow triangle sign) so you'll have to type YourModuleName.YourVariableName to access the variable. It keeps things a little bit cleaner.

So you can easily create a global array by creating the module, then clicking Add Property and declaring YourArrayName(-1) as Integer for example. You can add, remove, and modify any of the items in the array using the standard array functions (ubound, append, remove, etc).

mjdth
  • 6,536
  • 6
  • 37
  • 44