VB6 to Xojo array with years as index

I have some old VB6 code that I need to convert to Xojo.
How do I convert this?

Global AWISeries(1950 To 2120) As Single

where the index will be a year value in some calculations.

Arrays in Xojo always start with 0. You might instead want to use a Dictionary with the year as the Key.

And then use a loop to initialize the keys “1950” etc.?

Use a dictionary, or pairs

for x = 1950 to 2120 MyDictionary.value(x) = somevalue next

edit: oh… beaten to it while the site was down… :wink:

Thanks guys!

you can create an array with ubound 2100.

no problem and don’t mind the 2 KB of memory used for the unused indexes.

[quote]for x = 1950 to 2120
MyDictionary (x) = somevalue

If I do this I get the error:
This is not an array but you are using it as one.

I have about 5 similar arrays. Still don’t worry about all the xtra entries?

[quote=305910:@Tim Turner]If I do this I get the error:
This is not an array but you are using it as one.[/quote]
Well that’s what you get for copying and pasting code. Read the doc on Dictionary to learn how to use it.

I believe with what you’re trying to do, using a Dictionary will save you a lot of headaches over an array.

Edit: Formatting, and additional comment recommending Dictionary.

dim d as new dictionary for x = 1950 to 2120 d.value(x) = somevalue next

And what I get for typing from memory, hitting send, and going to make a coffee… :wink:
Fixed in situ

AWISeries(170) As Single //1950 To 2120

And call it in the code as


You can also create two functions ad hoc such as

Set :

Public Sub AWISeries(Year as Integer, assigns value as single) mAWISeries(Year-1950) = value End Sub


Public Function AWISeries(Year as Integer) as Single return mAWISeries(Year-1950) End Function

mAWISeries as single is a module or window property.

With this, you will not need to adapt VB calls to the array.

wasting 2000 *4 = 8 KB is not a big issues.
Especially if you never write those entries, the memory pages will never be allocated physically. You just reserved some address space.

It highly depends now what you do whether an array with extra entries, a dictionary or getter/setter methods are the best for performance or memory consumption.