fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC: Array index question

Array index question 1 year 5 months ago #1

  • boonnam
  • boonnam's Avatar Topic Author
  • Offline
  • Posts: 34
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

Please Log in or Create an account to join the conversation.

Array index question 1 year 5 months ago #2

Boonam,
there has to be more...
Made a new X#/Vo app, NO compiler switch set:
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?

Please Log in or Create an account to join the conversation.

Array index question 1 year 5 months ago #3

  • wriedmann
  • wriedmann's Avatar
  • Offline
  • Posts: 1426
  • Karma: 6
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:
__ARRAYBASE__

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:
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
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

Array index question 1 year 5 months ago #4

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
XSharp Development Team
The Netherlands
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

Array index question 1 year 5 months ago #5

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
XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.

Array index question 1 year 5 months ago #6

  • boonnam
  • boonnam's Avatar Topic Author
  • Offline
  • Posts: 34
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.

Please Log in or Create an account to join the conversation.

  • Page:
  • 1