xsharp.eu • value in USUAL does not support indexed operations
Page 1 of 2

value in USUAL does not support indexed operations

Posted: Mon Mar 13, 2023 1:46 pm
by ic2
We get a return value from a C# based 3rd party DLL which looks as follows when we inspect (tooltip) the usual uTot:
uTot System.String[,] (String[,]
[0,0] "NL"
[0,1] "7543"

(etc)

How can we access this in X#?

If we try it like with something like uSomething:= uTot[0,0] we get the error below:


'uTot[0,0]' threw an exception of type 'System.InvalidCastException'
Data: {System.Collections.ListDictionaryInternal}
HResult: -2147467262
HelpLink: null
InnerException: null
Message: "value in USUAL does not support indexed operations. The value needs to be an X# Array or implement the interface 'XSharp.IIndexedProperties'."
Source: "XSharp.RT"
StackTrace: " at XSharp.__Usual.get_Item(Int32 index1, Int32 index2)"
TargetSite: {XSharp.__Usual get_Item(Int32, Int32)}


Dick

value in USUAL does not support indexed operations

Posted: Mon Mar 13, 2023 2:56 pm
by robert
Dick,
Most likely, you have stored the return value of the C# call in an untyped variable (a USUAL)
Do you know what the C# call returns?
Please use that type for uTot, or else declare uTot with VAR:

var uTot := externalObject:MethodName()

The compiler will then query the object and retrieve its return type.

Robert

value in USUAL does not support indexed operations

Posted: Mon Mar 13, 2023 3:36 pm
by ic2
Hello Robert,

We already saw it was a string array when hovering on uTot:
uTot System.String[,] (String[,]

But we can't access it like we tried, with e.g. something like ? uTot[0,0] which has the value "NL" (also seen hovering)

Dick

value in USUAL does not support indexed operations

Posted: Mon Mar 13, 2023 4:31 pm
by Chris
Hi Dick,

Declare "uTot AS STRING[,]", does it work now?

(btw, that's an array, not an indexed expression)

value in USUAL does not support indexed operations

Posted: Tue Mar 14, 2023 11:23 am
by ic2
Hello Chris,

That works indeed, thanks.

One question: what is the difference with some of the other datatypes?

E.g. if I would define ux as usual and the value assigned is a normal string,

Code: Select all

? ux

would work without crash (where it doesn't for this string[,] datatype) and e.g.

Code: Select all

nMyIntVar:=ux+5


would fail as expected.

Dick

value in USUAL does not support indexed operations

Posted: Tue Mar 14, 2023 11:53 am
by Chris
Hi Dick,

Not sure if I understand what you mean, but assigning a strongly typed array to a usual it is indeed possible:

Code: Select all

LOCAL a AS STRING[]
a := STRING[]{10}

LOCAL u AS USUAL
u := a
But it's just not really useful doing that.

value in USUAL does not support indexed operations

Posted: Tue Mar 14, 2023 9:03 pm
by ic2
Hello Chris,

What I mean is this. Trying to get a value from the usual (although visible in VS' debugger tooltip) like uSomething:= uTot[0,0] we get the error as described earlier.

But if would use cSomething:=uSomething (where the first is a string and the 2nd a usual) would work (if the actual value of the usual is a string) , I think?

Dick

value in USUAL does not support indexed operations

Posted: Tue Mar 14, 2023 9:12 pm
by robert
Dick,
We have implemented the USUAL type very close to what exists inside VO.
And typed arrays like STRING[] did not exist in VO.

In theory, we could extend the USUAL type and add an indexer to that type. And then we could detect that the object inside the USUAL is a STRING[] or something similar, and then translate uValue[10] into retrieving the 10th element from the string array.

In fact, we already have an indexer for the USUAL type, but this indexes (like in VO) assumes that it should only work for usuals that contain an Xbase array. We did not think that would be necessary to support typed arrays, but it is not super difficult to do that.

Robert

value in USUAL does not support indexed operations

Posted: Tue Mar 14, 2023 10:30 pm
by Chris
Robert,

In VO, when you store a DIM array (which is close enough to .Net's arrays) in a USUAL, it is not possible to access element arrays with it. This code in VO:

Code: Select all

FUNCTION Start() AS INT
LOCAL DIM aaa[10] AS INT
LOCAL u AS USUAL
aaa[2] := 123
? aaa[2] // 123

u := aaa
? u[2] // Data Type error
produces a Data Type Error at runtime, so in order to maintain VO compatibility, I think also in X# it should continue throwing a runtime error, too.

value in USUAL does not support indexed operations

Posted: Wed Mar 15, 2023 10:11 am
by ic2
Hello Robert, Chris,

Thanks for explaining. It is not that I needed the behaviour to change, I was just curious why accessing usuals with e.g. a string worked. Now I know the background and also how it won't work with normal VO arrays either.

Dick