How to convert X# array to .Net array?

This forum is meant for questions and discussions about the X# language and tools
Post Reply
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

How to convert X# array to .Net array?

Post 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
User avatar
robert
Posts: 4520
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

How to convert X# array to .Net array?

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
Chris
Posts: 4906
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

How to convert X# array to .Net array?

Post 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
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

How to convert X# array to .Net array?

Post 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
Post Reply