Data conversion array->Object for excel

This forum is meant for questions and discussions about the X# language and tools
ecos
Posts: 91
Joined: Tue Nov 10, 2015 6:49 am

Data conversion array->Object for excel

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

Data conversion array->Object for excel

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
ecos
Posts: 91
Joined: Tue Nov 10, 2015 6:49 am

Data conversion array->Object for excel

Post by ecos »

meanwhile I saw that you do the same in _ArrayToObjectArray(). So I was not completely wrong...
User avatar
robert
Posts: 4225
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Data conversion array->Object for excel

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
ecos
Posts: 91
Joined: Tue Nov 10, 2015 6:49 am

Data conversion array->Object for excel

Post 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.
alex_schmitt
Posts: 85
Joined: Wed Jan 23, 2019 7:54 pm
Location: Germany

Data conversion array->Object for excel

Post 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
User avatar
Chris
Posts: 4562
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Data conversion array->Object for excel

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

.
Chris Pyrgas

XSharp Development Team test
chris(at)xsharp.eu
FdeRaadt
Posts: 31
Joined: Wed Sep 01, 2021 12:49 pm

Data conversion array->Object for excel

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

Data conversion array->Object for excel

Post by robert »

Frank,
Your code is not complete.
How is aData typed ?

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
alex_schmitt
Posts: 85
Joined: Wed Jan 23, 2019 7:54 pm
Location: Germany

Data conversion array->Object for excel

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