web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Byte() in AX4 - How receibed Byte() ArrayofByte by parm on Microsoft Dynamnics AX4

(0) ShareShare
ReportReport
Posted on by 150

Version Microsoft Dynamics Axapta 4

 

From an external application in vb.NET and, using business connector, it is being invoked a method from AX class, this method receive a string (xml) as a parameter, transformed to byte()

How can I receive the parameter in AX4 due to the fact that the type Byte[] doesn´t exist?

Call code from .NET:

Código llamada desde .NET:

axapta.LogonAs("XXXXXXX", "dynamics.gvm", strNetCredProxy, "", "", "", pathIS)

 

MsgBox("LOGIN OK")

showException = True

axaptaObject = axapta.CreateAxaptaObject("DMPClassTestingPaco")

str = axaptaObject.Call("RecibeArrayBytesCinco", _arrbyte)

  

 

Class Axapta DMPClassTestingPaco.RecibeArrayBytesCinco()

public str RecibeArrayBytesCinco(ClrObject _byteArray)

{

    System.Array            byteArray;

    System.Text.Encoding    encoding;

    str                     string;

    int                     byte, i, arrayLength;

    container               arrayofBytes;

    CodeAccessPermission    permission;

    ;

    permission  = new InteropPermission(InteropKind::ClrInterop);

    permission.assert();

   

 

    //string = "Sample string";

    //encoding = System.Text.Encoding::get_UTF8();

    //byteArray = encoding.GetBytes(string);

 

    byteArray = _byteArray;

 

    arrayLength = byteArray.get_Length();

    for (i = 0; i < arrayLength; i++)

    {

        byte = byteArray.GetValue(i);

        info(strfmt("%1", byte));

    }

}

 

Always throw Error:

-Thrown: "Error executing code: Array object has no method 'get_Length'.

Stack Trace

(C)\Classes\Array\get_Length

(C)\Classes\DMPClassTestingPaco\RecibeArrayBytesCinco - line 20

" (Microsoft.Dynamics.BusinessConnectorNet.BusinessConnectorException)      Exception Message = "Error al ejecutar código: Array objeto no tiene el método 'get_Length'.\n\nSeguimiento de pila\n\n(C)\\Classes\\Array\\get_Length\n(C)\\Classes\\DMPClassTestingPaco\\RecibeArrayBytesCinco - line 20\n", Exception Type = "Microsoft.Dynamics.BusinessConnectorNet.BusinessConnectorException"

 

thank you all for your help

 

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,908 Most Valuable Professional on at

    In AX2009 you can use System.Byte[] type. I don't remember if this is supported in AX4. If not, use weak typing and declare byteArray as CLRObject.

  • Francisco Nestares Profile Picture
    150 on at

    Hello,

    Unfortunately AX4 not supported byte ().

    I know not weak typing, you could send me some example

    Thank you very much in advance

  • Martin Dráb Profile Picture
    237,908 Most Valuable Professional on at

    I tried your code in AX4 and it works. Therefore I expect you don't get System.Array through the BC call.

    If the call stack is correct, _byteArray is actually X++ Array class, not System.Array class.

  • Francisco Nestares Profile Picture
    150 on at

    Hello, first of all, thank you very much by your sugestion. I changed System.Array by Array and it works.
    Now, I have Array of byte and I got to transform to container Ax.

    Do you now, how can i to transfor the bytes to characters?
    The idea is receive by parm a byte[] of xml and convert it the byte[] to xml again.


    //AX4 doesn't support arrays in that fashion.

    public str RecibeArrayBytesSiete(Array _byteArray) //Desde bc devuelve el tamaño con lastIndex();
    {
        #define.ClrMemoryStream                     ('System.IO.MemoryStream')
        #define.ClrStreamReader                     ('System.IO.StreamReader')
        System.IO.Stream        stream;
        System.String           line;
        System.IO.BinaryReader  byreader;
        System.IO.MemoryStream  memoryStream;
        System.IO.StreamReader  reader;
        //CLRObject               byteArray;
        System.Text.Encoding    encoding;

        //System.Array            byteArray;

        CLRObject               byteArrayObj;
        str                     string,stringDos;
        int                     byte, i, arrayLength;
        container               cont;
        int                     x[];
        //ClrObject               byteArray  = new ClrObject("System.Byte");

       
        System.Type typeOfByte;
        System.Array arrayOfByte;
        int arrayOfByteLength = 1;
        Array                   byteArray;
        System.Byte             singleByte;
        str                     a[];
        container               cData;
        ContainerClass          cc;
        System.String           lines;
        ;


        arrayOfByteLength = _byteArray.lastIndex();

        //return int2str(arrayOfByteLength);

        for( i = 1; i <= arrayOfByteLength; ++i )
        {
            //cont = conins(cont,i+1,(ClrInterop::getAnyTypeForObject(arrayOfByte.GetValue( i ) )));
            cont = conins(cont,i,  queryvalue(_byteArray.value(i)));
        }

         //HOW TO CONVERT BYTE()/CONTAINERofBYTE TO ORIGINAL STR (XML)

    }

  • Martin Dráb Profile Picture
    237,908 Most Valuable Professional on at

    Wait. If you start with a string and want to have a string at the and as well, why don't you just use string all the time?

  • Francisco Nestares Profile Picture
    150 on at

    Hello, it is a good question.

    We have a external aplication, it sends a str and in AX we receive  the str to perform some operations.

    Two weeks ago we had a problem regarding memory size because the str was too long:

    Error executing code: Insufficient memory to run script.

    Error executing code:  object not initialized.

     

    Stack trace

     

    (C)\Classes\\budgetInterfaceForIAS

    After some investigations we could solve the problem increasing the RECORD KEY maxbuffersize in .axc, but it is not a good practice.

    The external aplication works with aplications other than AX and, in order to send this kind of data they use byte() because is smaller.

    Because of this, we are trying to change the code to transfer byte instead of a str and transforn to str again afterwards.

  • Martin Dráb Profile Picture
    237,908 Most Valuable Professional on at

    But you'll get the same error when you convert the byte array to X++ string and try to assign it to a variable, so you just do a lot of additional conversions without solving the problem. Look at my blog post about Insufficient memory.

  • Francisco Nestares Profile Picture
    150 on at

    Actually I understud the same, that reassembling xml string, the error would appear  again; but maybe I could assing it to several variables when converting.

    We will try to reduce the size of the data.

    thank you very much for your help.

  • Martin Dráb Profile Picture
    237,908 Most Valuable Professional on at

    If you read my post carefully, you'll see that you can bypass the check and assign unlimited amount of data to a variable. But what would you do with it? If you want to pass it to a method, you have to assign it at once to the method argument and you're back in the same problem.

    If you want to work with smaller parts, you can achieve the same by splitting the original string to several smaller ones.

    To convert the byte array back to string, you should use GetChars() of the same Encoding as used to create the array. If it gives you nothing better than passing a (sub)string directly, it's just wasting of resources.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans