value in USUAL does not support indexed operations

This forum is meant for questions and discussions about the X# language and tools
ic2
Posts: 1798
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

value in USUAL does not support indexed operations

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

value in USUAL does not support indexed operations

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
ic2
Posts: 1798
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

value in USUAL does not support indexed operations

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

value in USUAL does not support indexed operations

Post by Chris »

Hi Dick,

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

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

XSharp Development Team test
chris(at)xsharp.eu
ic2
Posts: 1798
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

value in USUAL does not support indexed operations

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

value in USUAL does not support indexed operations

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

XSharp Development Team test
chris(at)xsharp.eu
ic2
Posts: 1798
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

value in USUAL does not support indexed operations

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

value in USUAL does not support indexed operations

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
Chris
Posts: 4573
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

value in USUAL does not support indexed operations

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

XSharp Development Team test
chris(at)xsharp.eu
ic2
Posts: 1798
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

value in USUAL does not support indexed operations

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