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

TOPIC:

Data conversion array->Object for excel 18 Jan 2022 11:37 #21149

  • ecos
  • ecos's Avatar
  • Topic Author


  • Posts: 40
  • 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

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

    Data conversion array->Object for excel 19 Jan 2022 08:44 #21156

    • robert
    • robert's Avatar


  • Posts: 3276
  • 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

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

    Data conversion array->Object for excel 19 Jan 2022 08:47 #21157

    • ecos
    • ecos's Avatar
    • Topic Author


  • Posts: 40
  • meanwhile I saw that you do the same in _ArrayToObjectArray(). So I was not completely wrong...

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

    Data conversion array->Object for excel 19 Jan 2022 08:59 #21158

    • robert
    • robert's Avatar


  • Posts: 3276
  • 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:
    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

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

    Data conversion array->Object for excel 19 Jan 2022 09:30 #21160

    • ecos
    • ecos's Avatar
    • Topic Author


  • Posts: 40
  • 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.

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

    Data conversion array->Object for excel 08 Jun 2022 16:57 #22714

    • alex_schmitt
    • alex_schmitt's Avatar


  • Posts: 53
  • 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."
    LOCAL aArray AS ARRAY
    LOCAL oStringArray AS System.String[]
    
    aArray:=ArrayNew(10)
    oStringArray:=aArray

    What is wrong here?

    Best,
    Alex

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

    Data conversion array->Object for excel 08 Jun 2022 17:10 #22715

    • Chris
    • Chris's Avatar


  • Posts: 3739
  • 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.

    .
    XSharp Development Team
    chris(at)xsharp.eu

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

    Data conversion array->Object for excel 16 Jun 2022 12:52 #22761

    • FdeRaadt
    • FdeRaadt's Avatar


  • Posts: 31
  • I tried to following code in my XSharp project:
    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
    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.

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

    Data conversion array->Object for excel 16 Jun 2022 15:39 #22767

    • robert
    • robert's Avatar


  • Posts: 3276
  • Frank,
    Your code is not complete.
    How is aData typed ?

    Robert
    XSharp Development Team
    The Netherlands

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

    Data conversion array->Object for excel 21 Jun 2022 22:01 #22819

    • alex_schmitt
    • alex_schmitt's Avatar


  • Posts: 53
  • Apologies for the delay I have been "out" the last weeks ;)

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

    This one...
    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:
    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?

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

    Data conversion array->Object for excel 22 Jun 2022 01:37 #22820

    • Chris
    • Chris's Avatar


  • Posts: 3739
  • HI Alex,

    You can't just cast a OBJECT[] to a STRING[], those are not compatible. Instead, you need to write code that enumerates the OBJECT[] returned by _ArrayToObjectArray() and put each item to the STRING[]. If this still doesn't work, please post a complete code snippet so we can have a look.

    About your other function, the correct way to instantiate a STRING[] is this:

    output:=System.String[]{ALen(input)}

    .
    XSharp Development Team
    chris(at)xsharp.eu

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

    Last edit: by Chris.

    Data conversion array->Object for excel 22 Jun 2022 21:28 #22825

    • alex_schmitt
    • alex_schmitt's Avatar


  • Posts: 53
  • Hi Chris,

    Thanks, that was doing the trick!

    Then this is the successful solution
    FUNCTION XSStringArray2CSStringArray(input as ARRAY) AS System.String[]
    
    LOCAL output as System.String[]
    local i as int
    
    output:=System.String[]{ALen(input)}
    
    for i:=1 to ALen(input)
    	output[i]:=(System.String)input[i]
    next
    
    RETURN output

    I have to admit I would not have guessed that the syntax for instantiating an array of string objects is being instantiated that way
    but maybe I didn't dig deep enough in the help docs.

    Best,
    Alex

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

    Data conversion array->Object for excel 23 Jun 2022 00:49 #22827

    • Chris
    • Chris's Avatar


  • Posts: 3739
  • Hi Alex,

    Well, all types instantiate with TypeName{}. Since the type name here is STRING[], then instantiating it with STRING[]{} sort of makes sense ;)

    .
    XSharp Development Team
    chris(at)xsharp.eu

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

    Data conversion array->Object for excel 23 Jun 2022 13:16 #22835

    • FFF
    • FFF's Avatar


  • Posts: 1391
  • That I call a short, clear, concise and understandable explanation!
    Thx for that.
    Regards
    Karl (X# 2.14.0.4; Xide 1.33; W8.1/64 German)

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

    Data conversion array->Object for excel 23 Jun 2022 22:57 #22852

    • alex_schmitt
    • alex_schmitt's Avatar


  • Posts: 53
  • @Chris: Well, yes ;) But as I was always instantiating arrays with ArrayNew() I was not able to do this transfer ;)

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

    • Page:
    • 1