xsharp.eu • Data conversion array->Object for excel
Page 1 of 2

Data conversion array->Object for excel

Posted: Tue Jan 18, 2022 10:37 am
by ecos
Hi all,
in VO we could assign a 2-dimensional array to excel.range.value. x# does not like that.
I found a solution to make it work:

// aData is a 2-dimensional array
nDimX := ALen(aData[1])
nDimY := ALen(aData)

cOL := "A1"
cUR:= CHR(64+nDimX)+ntrim(nDimY)

oRange := (Microsoft.Office.Interop.Excel.Range)oWorksheet:Range[ cOL, cUR]
oData := OBJECT[,]{nDimY,nDimX}
FOR nY := 1 UPTO nDimY
FOR nX := 1 UPTO nDimX
oData[nY,nX] := aData[nY][nX]
NEXT
NEXT
oRange:Value := oData

Is there a faster (better) way to convert the array to an Object[,] ?

TIA
Karl

Data conversion array->Object for excel

Posted: Wed Jan 19, 2022 7:44 am
by robert
Karl,
ecos wrote:Hi all,
Is there a faster (better) way to convert the array to an Object[,] ?
Not that I know of.

Robert

Data conversion array->Object for excel

Posted: Wed Jan 19, 2022 7:47 am
by ecos
meanwhile I saw that you do the same in _ArrayToObjectArray(). So I was not completely wrong...

Data conversion array->Object for excel

Posted: Wed Jan 19, 2022 7:59 am
by robert
Karl,
We do have an implicit converter in the array class to convert an array to to object[].
I am not sure if this handles multi dimensional arrays exactly as you expect:

Code: Select all

FUNCTION Start AS VOID
    LOCAL a AS ARRAY
    LOCAL o AS OBJECT[]
    a := ArrayNew(10)
    AFill(a, 42)       
    o := a
    FOREACH VAR x IN o
        ? x
    NEXT
    WAIT
RETURN

Robert

Data conversion array->Object for excel

Posted: Wed Jan 19, 2022 8:30 am
by ecos
Robert,
that is what I tried first, but thus oRange:Value := o crashes.
Anyway I only asked to learn how to do things better. Everything works fine now so no need to change anything.

Data conversion array->Object for excel

Posted: Wed Jun 08, 2022 2:57 pm
by alex_schmitt
Hey all,

picking up this aging thread with a similar question:

I want to convert an XS Array to a C# Array for handing over to and using it in my C# function.

If I understand Robert correctly, this implicit conversion should work, but it throws an System.InvalidCastException: "Das Objekt des Typs "XSharp.__Array" kann nicht in Typ "System.String[]" umgewandelt werden."

Code: Select all

LOCAL aArray AS ARRAY
LOCAL oStringArray AS System.String[]

aArray:=ArrayNew(10)
oStringArray:=aArray


What is wrong here?

Best,
Alex

Data conversion array->Object for excel

Posted: Wed Jun 08, 2022 3:10 pm
by Chris
Hi Alex,

Don't you get a compiler error with this code?

The problem is that you need to use OBJECT[] instead of STRING[]. The implict conversion code cannot know at compile time the type of the members of the VO ARRAY, so it converts to a generic OBJECT array.

I think the best solution in any case is not to rely in this automatic conversion, instead write your small code that does it, which can also be strongly typed to the actual values used etc.

.

Data conversion array->Object for excel

Posted: Thu Jun 16, 2022 10:52 am
by FdeRaadt
I tried to following code in my XSharp project:

Code: Select all

oData := OBJECT[,]{nY,nX}
FOR nY := 1 UPTO ALen(aData)
FOR nX := 1 UPTO ALen(aData[1])
oData[nY,nX] := aData[nY][nX]
NEXT
NEXT
oRange:Value := aData
This results in an Error: XS0021 Cannot apply indexing with [] to an expression of type 'object'

I've tried a different approach

Code: Select all

				
FOR nY := 1 UPTO ALen(aData)
FOR nX := 1 UPTO ALen(aData[1])
oRange := oWorkSheet:Range[ExcelCol(nX)+NTrim(nY),ExcelCol(nX)+NTrim(nY)]
oRange:Value := aData[nY][nX]
NEXT
NEXT
Note: ExcelCol is a Function I have added manually.
This works but I'm debating if this would lead to performance issues in the near future.

Data conversion array->Object for excel

Posted: Thu Jun 16, 2022 1:39 pm
by robert
Frank,
Your code is not complete.
How is aData typed ?

Robert

Data conversion array->Object for excel

Posted: Tue Jun 21, 2022 8:01 pm
by alex_schmitt
Apologies for the delay I have been "out" the last weeks ;)

Still struggling with the conversion from ARRAY to System.String[]

This one...

Code: Select all

FUNCTION XSStringArray2CSStringArray(input as ARRAY) AS System.String[]
LOCAL output as System.String[]
local i as int
output:=(String[])_ArrayToObjectArray(input)
RETURN output
is throwing an invalid cast exception:

System.InvalidCastException
HResult=0x80004002
Nachricht = Das Objekt des Typs "System.Object[]" kann nicht in Typ "System.String[]" umgewandelt werden.

Other attempt:

Code: Select all

FUNCTION XSStringArray2CSStringArray(input as ARRAY) AS System.String[]

LOCAL output as System.String[]
local i as int

output:=System.String{['']}[alen(input)] // <-- something is wrong here ;)

for i:=1 to ALen(input)
	output[i]:=(System.String)input[i]
next

RETURN output
Other idea?