xsharp.eu • How to convert X# array to .Net array?
Page 1 of 1

How to convert X# array to .Net array?

Posted: Tue Oct 03, 2017 10:25 pm
by ic2
Another array question...

We need to pass an array to a C# method which puts the array values into Excel. However, the code to convert an X# array to a system array still gives a System.IndexOutOfRangeException: 'Index was outside the bounds of the array.' error, also if we start aNew on index 0] (see code below).

What could be wrong?

Dick

LOCAL nRegel, nKolom,i,j AS dword
nRegel := alen(aData)
nKolom := alen(aData[1])
LOCAL aNew AS STRING[,]
aNew := STRING[,]{nRegel,nKolom}
FOR i := 1 TO nRegel
FOR j := 1 TO nKolom
if IsString(aData[i,j])
aNew[i-1,j-1] := aData[i,j]
ELSE
aNew[i,j] := ""
endif
next j
next i

How to convert X# array to .Net array?

Posted: Wed Oct 04, 2017 5:07 am
by robert
Dick,
Dick wrote:

Code: Select all

if IsString(aData[i,j])
aNew[i-1,j-1] := aData[i,j]
ELSE
aNew[i,j] := ""
endif
The first assignment uses aNew[i-1, j-1] syntax. The second assignment aNew[i,j].
The second is Ok. The compiler already inserts the -1 for you because it knows this is a real array.

When you doubt then reflector or ILSpy are your friends: you can always check what the compiler has generated with these.

Robert

How to convert X# array to .Net array?

Posted: Wed Oct 04, 2017 6:14 am
by Chris
Hi Dick,

In addition to what Robert said, I think you got confused because of the talk that while VO arrays are 1-based, the .Net arrays are 0-based. What really happens, is that in x# (and in vulcan), you have an option to make the compiler treat arrays (VO and .Net style) always as 1-based, in a completely transparent way, so you can continue working with them exactly the same way as you did in VO (the equivalent of .Net arrays in VO is the DIM array). Furthermore, you can simply share .Net arrays (but not VO-arrays) between x#, c#, vulcan etc code without any special treatment, the same array will be treated by x# as 1-based and by c# as 0-based, but that's just semantics and you don't need to worry about it.

What's different though is collections, classes like ArrayList, List, SortedList etc. Those are very often being used like arrays, but in reality they are very different to them. Those are the ones that are always 0-based, no matter the compiler language that they are used with. (Well, to be more precise, it's up to the person who implemented them if they will be 0-based or not, but the general convention in .Net is to implement them (unfortunately IMO) almost always as 0-based).

Chris

How to convert X# array to .Net array?

Posted: Thu Oct 05, 2017 9:37 am
by ic2
Hello Robert, Chris,

Thank you for your lighting fast reply (posted late in the evening, reply early in the morning).

My employee working on this project tried using the array for creating an Excel sheet with and without the "0 base correction" and at that moment none of the solution worked. After a while he got it working but it was good to know which of the 2 options was the right one to start solving the problem with.

Dick