3

I am using a C# application to do some work and send back the results in AX via a service.

I've created a few classes in AX and use their instances in C# then I send the objects back with the help of a classic array.

In AX I receive the stuff in a System.Collections.ArrayList and here comes my question:

How can I iterate over this collection and check the objects type?

for (...)
{
   if (arr[i] is SalesLineCSharp) 
   {
   } 
   else if (arr[i] is SalesTableCSharp)
   {
   }
   //etc....
}

Something like is or as ?

I've just made an example and tried this ..

info(strFmt("%1", classId2Name(classIdGet(arr.get_Item(i)))));

Indeed for the custom types I get the name of the class and for strings and ints I get CLRObject, but that looks so bad.

Is there a cleaner way to accomplish this?

DAXaholic
  • 33,312
  • 6
  • 76
  • 74
Olaru Mircea
  • 2,570
  • 26
  • 49

1 Answers1

4

There are actually casting operators in X++ starting from AX 2012 - see here on MSDN.

So you should be able to do something like

Object tmpItem;
...
tmpItem = arr.get_Item(i);
if (tmpItem is SalesLineCSharp)
{
    ...
DAXaholic
  • 33,312
  • 6
  • 76
  • 74
  • I tried that before posting the question and i got the following error : The type get_Item must designate a compatible class, an interface, or a table. I really don't know how to tackle this message. – Olaru Mircea Jul 26 '16 at 10:47
  • What happens if you assign the return value of get_Item to an intermediate variable of type 'Object' and use this var to check for the types. Maybe the auto. marshalling between .NET and X++ types can't handle it directly – DAXaholic Jul 26 '16 at 10:48
  • 1
    Sometimes we just have to help the compilers :) – DAXaholic Jul 26 '16 at 10:51