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

TOPIC: How to convert X# array to .Net array?

How to convert X# array to .Net array? 1 year 9 months ago #2812

  ic2's Avatar Topic Author ic2 Offline Posts: 521
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

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

How to convert X# array to .Net array? 1 year 9 months ago #2813

  robert's Avatar robert Offline Posts: 1048
Dick,

Dick wrote:

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
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.

Last edit: by robert.

How to convert X# array to .Net array? 1 year 9 months ago #2814

  Chris's Avatar Chris Offline Posts: 1226
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
XSharp Development Team
chris(at)xsharp.eu

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

How to convert X# array to .Net array? 1 year 9 months ago #2817

  ic2's Avatar Topic Author ic2 Offline Posts: 521
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

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

  • Page:
  • 1