Search code examples
axaptamicrosoft-dynamicsdynamics-ax-2012x++dynamics-ax-2012-r3

Check objects type


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?


Solution

  • 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)
    {
        ...