Assign values indirectly

Hi. In the dialect of Basic I am coming from you can assign values to variables by referring to their names in a string. For example, say I have 10 integer variables named myvar1 through myvar10 and I want to set them all to zero:
for i = 1 to 10
execute(“LET myvar”+STR(i)+"=0")
next i

Is there an equivalent function in Xojo?

No, and (without knowing for sure) that’s the difference between an interpreted language and a compiled one.

However, you can make myvar an array and do the same thing in a much simpler way:

dim myvar( 9 ) as integer // Arrays start at index 0
// Do some stuff that changes the values of the array
// Now you want to clear all the values
redim myvar( -1 ) // Empty array
redim myvar( 9 ) // All initialized to 0

Hi Kem, thanks for the reply. My previous Basic was indeed interpreted, not compiled. The example I provided was a bit too simplistic, and I agree using an array for that specific example would work. What I was hoping to be able to do is iterate through a Dictionary containing variable names and set values using those names. I will find a work around though.

Again, I don’t need the details, but it seems like you can skip the middleman and just use the Dictionary directly.

dim d as new Dictionary
d.Value( "myvar1" ) = 2
// Later..
d.Value( "myvar1" ) = 3
if d.Lookup( "myvar2", 0 ) = 10 then
  d.Value( "myvar2" ) = 20
end if

If that’s what you want, why do these items have to a variable? Just use a dictionary and the “key” is the “variable” name and the value is the value.

I do this all the time. For instance, in the old days I had a different variable for each setting in my app’s Preference panel. So there would be booleans, numbers, strings, etc. As my app grew, I had several dozens settings, each with their own variable. It was difficult to manage. Now I just have everything in a gPreferences dictionary. I have names I use for each of the settings and I can get and retrieve them as needed from anywhere in my app. What’s really nice is how expandable this is because I can just add new settings easily without having to allocate new variables.

If you need a lot of variables, especially if they’re different types, a dictionary is the way to go. (If they’re all the same type, an array might work better, though you then have to remember which index is which.)

In addition, you can clear all the variables pretty easily as long as you get used to using Lookup to retrieve values. For example:

d = new Dictionary
d.Value( "myvar1" ) = 4
// Later
d = new Dictionary // All cleared
d.Value( "myvar1" ) = d.Lookup( "myvar1", 0 ) + 10 // No exception, and new value stored

Thanks for the input - it is very helpful! I agree that using the dictionary directly makes a lot of sense.

Another tip is that you can step through a dictionary via its keys property, so it can also work similar to an array.

You might be looking for introspection (sometimes called reflection)

Create these two methods in a module:

[code]Sub introspectSetProperty(extends wv as webObject, propertyName as string, assigns v as variant)
dim mProperties() as Introspection.PropertyInfo = Introspection.GetType(wv).getProperties
For j as integer = 0 to ubound(mProperties)
if mProperties(j).name = propertyName then
mProperties(j).Value(wv) = v
return
end
Next
End Sub

Function introspectProperty(extends wv as webObject, propertyName as string, altValue as variant = nil) As variant
dim mProperties() as Introspection.PropertyInfo = Introspection.GetType(wv).getProperties
For j as integer = 0 to ubound(mProperties)
if mProperties(j).name = propertyName then
return mProperties(j).Value(wv)
end
Next
return altValue
End Function

[/code]

And then you can do things like this:

[code]someObject.introspectSetProperty(“BGImage”) = BackgroundImage

dim someBool as boolean = someObject.introspectProperty(“Collapsed”,false).booleanValue
[/code]