Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics NAV (Archived)

Splitting a long text string into shorter ones, and a funny bug

(0) ShareShare
ReportReport
Posted on by 360

After this nice 2013 R2 we have been able to use text strings of unlimited length.
This is very handy, but it also causes a need to split them into smaller chunks.
I created a function that does exactly that, but bumped into a funny bug:
It seems that if you give an array as a parameter to a function, called function sets the size of the local array to match the size of the array in calling function!

The POC code below has an array of 100 in the calling function, and an array of 2 in the called function, and yet it seems to be working fine! (At least in my NAV 2013 R2 build 36897)

This is funny, since it eliminates the need to declare an array in a function way too big. I would have reported this to Microsoft also, but it seems that I would have to pay 299€ for that, since we don't seem to have any technical credits left :D

Please find stringwrapper function below, and enjoy!

//Urpok




OBJECT Codeunit 50101 TestCodeunit
{
  OBJECT-PROPERTIES
  {
    Date=04.07.14;
    Time=10:58:00;
    Modified=Yes;
    Version List=INN000;
  }
  PROPERTIES
  {
    OnRun=VAR
            tempArray@1000000004 : ARRAY [100] OF Text;
            OK@1000000002 : Boolean;
            i@1000000001 : Integer;
            MsgText@1000000000 : Text;
          BEGIN
            CLEAR(tempArray);
            tempArray[1] := 'This is a long text i want to wrap to lines with max 5 characters long';
            OK := WrapText(tempArray,5);
            i := 0;
            REPEAT
              i +=1;
              MsgText += FORMAT(i) + ': "' + tempArray[i] + '"\';
            UNTIL(STRLEN(tempArray[i])=0);

            MESSAGE('Success:' + FORMAT(OK) + '\' + MsgText);
          END;

  }
  CODE
  {

    PROCEDURE WrapText@1000000000(VAR inText@1000000000 : ARRAY [2] OF Text;MaxLen@1000000001 : Integer) OK : Boolean;
    VAR
      i@1000000002 : Integer;
    BEGIN
      //Spreads the first line of inText array to other lines as MaxLen long strings, returns ok if no overflow
      IF (ARRAYLEN(inText) < 2) THEN
        ERROR('Array must be bigger than 1');
      IF (MaxLen < 1) THEN
        ERROR('MaxLen has to be a positive integer');
      FOR i := 1 TO ARRAYLEN(inText)-1 DO BEGIN
        inText[i+1] := COPYSTR(inText[i],MaxLen+1);
        inText[i]   := COPYSTR(inText[i],1,MaxLen);
        IF (STRLEN(inText[i+1]) <= MaxLen) THEN
          EXIT(TRUE);
      END;
      EXIT (STRLEN(inText[i+1]) <= MaxLen);
    END;

    BEGIN
    END.
  }
}

*This post is locked for comments

  • keoma Profile Picture
    keoma 32,675 on at
    RE: Splitting a long text string into shorter ones, and a funny bug

    nice code snippet, worth a blog posting.

    best regards

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans