Page 1 of 1
Array index question
Posted: Mon Oct 02, 2017 8:14 pm
by boonnam
I am testing my converted code from VO. I am using VO dialect with "Use Zero Based Arrays" set to false.
Here is a visual representation of aIntRate array (from VO debugger):
[1]
[1]=01/01/2017
[2]=3.0000
[3]=3.0000
[2]
[1]=09/29/2017
[2]=3.0000
[3]=3.0000
Here is the line that crash with index out of range error in X#:
dIntEff := aIntRate[ Min(n + 1, nMax), 1 ]
When this code run, on the first loop, the value for n is 1. The value for aMax is 2. So in this case, I am looking for the aIntRate[2,1] value, which is 09/29/2017. This is the result in VO.
When I run this same code against the same data, I get index out range error in X#. But if I decrement both n and nMax before this line run, I get the same result as in VO. Now my X# code look like this:
n--
nMax--
dIntEff := aIntRate[ Min(n + 1, nMax), 1 ]
This tell me the index under X# is zero base despite the setting. We have lot of arrays in our application. Any idea how to make sure the index is 1 base?
Thanks,
Boonnam
Array index question
Posted: Mon Oct 02, 2017 9:01 pm
by FFF
Boonam,
there has to be more...
Made a new X#/Vo app, NO compiler switch set:
Code: Select all
FUNCTION Start( ) AS VOID
VAR x := {}
AAdd(x, {1,2.3} )
AAdd(x, {4,5,6})
// ? x[0,1]
VAR n:=1
VAR nMax:=2
? Min (n+1, nmax)
? x[ Min(n + 1, nMax), 1 ]
? x[2,1]
RETURN
This works.
Uncommenting the access to item 0 the app crashs at runtime.
Did you add something like above in your code to crosscheck?
Array index question
Posted: Tue Oct 03, 2017 3:48 am
by wriedmann
Hi Boonnam,
I suspect there is something wrong with the compiler switches.
I have moved a lot of VO code, and have no problem with it (regarding arrays), and one of them is used daily.
You could output the following compiler macro:
to see how your code is compiled.
Wolfgang
P.S. this is a sample how to write code independently from the /az compiler switch, using the __ARRAYBASE__ compiler macro:
Code: Select all
local aStrings as string[]
local nI as int
local nLen as int
System.Console.WriteLine("Code independent from /az compiler setting")
if __ARRAYBASE__ == 0
System.Console.WriteLine("0-based arrays")
else
System.Console.WriteLine("1-based arrays")
endif
aStrings := <string> { "element 1", "element 2", "element 3", "element 4", "element 5" }
// foreach works independently from the /az setting
foreach cString as string in aStrings
System.Console.WriteLine( String.Format( "member is {0}", cString ) )
next
nLen := aStrings:Length - 1 + __ARRAYBASE__
for nI := ( __ARRAYBASE__ ) upto nLen
System.Console.WriteLine( String.Format( "member {0} is {1}", nI:ToString(), aStrings[nI] ) )
next
Array index question
Posted: Tue Oct 03, 2017 7:02 am
by robert
Boonnam,
Can you show us a little bit more of your code:
- What is the datatype of the variable where the array is stored (aIntRate)?
- What is the datatype of the array indexer (n)
Robert
Array index question
Posted: Tue Oct 03, 2017 2:18 pm
by Chris
Boonam,
First of course, please make sure you are using the latest build 1.0.3, because in 1.0.2 there was indeed a bug relates to arrays in specific cases.
If you are already using that, then to sum up the suggestions everybody made to you already, please set your app to type console application and surround the code like that to give some info when the error happens:
TRY
dIntEff := aIntRate[ Min(n + 1, nMax), 1 ]
CATCH e AS Exception
? "Execption caught"
? "__ARRAYBASE__ =", __ARRAYBASE__
? "n =" , n
? "nMax =" , nMax
? "Min(n + 1, nMax) =", Min(n + 1, nMax)
? "ALen(aIntRate) =", ALen(aIntRate)
? "ALen(aIntRate[1]) =", ALen(aIntRate[1])
? "ALen(aIntRate[2]) =", ALen(aIntRate[2])
END TRY
what are the values that are being printed?
Chris
Array index question
Posted: Tue Oct 03, 2017 3:13 pm
by boonnam
Thank you everyone for the reply. I knew I didn't have this issue with version 1.01. I was using version 1.02. Version 1.03 does fix this issue.