Data conversion array->Object for excel
Data conversion array->Object for excel
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
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
Karl,
Robert
Not that I know of.ecos wrote:Hi all,
Is there a faster (better) way to convert the array to an Object[,] ?
Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
The Netherlands
robert@xsharp.eu
Data conversion array->Object for excel
meanwhile I saw that you do the same in _ArrayToObjectArray(). So I was not completely wrong...
Data conversion array->Object for excel
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:
Robert
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
The Netherlands
robert@xsharp.eu
Data conversion array->Object for excel
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.
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.
-
- Posts: 85
- Joined: Wed Jan 23, 2019 7:54 pm
- Location: Germany
Data conversion array->Object for excel
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."
What is wrong here?
Best,
Alex
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
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.
.
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
chris(at)xsharp.eu
XSharp Development Team
chris(at)xsharp.eu
Data conversion array->Object for excel
I tried to following code in my XSharp project:
This results in an Error: XS0021 Cannot apply indexing with [] to an expression of type 'object'
I've tried a different approach
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.
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
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
This works but I'm debating if this would lead to performance issues in the near future.
Data conversion array->Object for excel
Frank,
Your code is not complete.
How is aData typed ?
Robert
Your code is not complete.
How is aData typed ?
Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
The Netherlands
robert@xsharp.eu
-
- Posts: 85
- Joined: Wed Jan 23, 2019 7:54 pm
- Location: Germany
Data conversion array->Object for excel
Apologies for the delay I have been "out" the last weeks ![Wink ;)](https://dt9qzg9h465rt.cloudfront.net/phpBB3/images/smilies/icon_e_wink.gif)
Still struggling with the conversion from ARRAY to System.String[]
This one...
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:
Other idea?
![Wink ;)](https://dt9qzg9h465rt.cloudfront.net/phpBB3/images/smilies/icon_e_wink.gif)
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
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