Digital Metaphors Report Builder 11.05: why my DELPHI code crashes without any error?

StackOverflow https://stackoverflow.com/questions/15688515

  •  30-03-2022
  •  | 
  •  

Question

Now I build a template for our invoice printer. But I do really not know, why it crashes without any error.

My goal is to separate the String ItemName at the ';' and print each part into a new line to a Memo1.

procedure DetailBeforeGenerate;
var
  s1: String;
  s2: String;
  wordcount: Integer;
  notelength: Integer;

begin
 s1 := plPrintInvLine['ItemName'];
 notelength := Length(s1);
while notelength > 0 do
begin
    notelength := Length(s1);
    wordcount := Pos(';' , s1);
    s2 :=   Copy(s1, 0, wordcount-1);
    Memo1.Lines.Add(s2);
    Delete(s1, 0, wordcount);
end;

end;
Was it helpful?

Solution

See comments below regarding accessing index[0] in a string and thanks to David Heffernen and Ken White. BUT:

Looks like you've got an infinite loop in your code:

notelength := Length(s1);
while notelength > 0 do
begin
    notelength := Length(s1);
    wordcount := Pos(';' , s1);
    s2 :=   Copy(s1, 0, wordcount-1);
    Memo1.Lines.Add(s2);
    Delete(s1, 0, wordcount);
end;

Delete(s1, 0, wordcount); Has no effect! Try it in Delphi debugger. Result? notelength is never decremented so you'll loop forever. ' Delete(s1, 0, wordcount);' does not blow up but neither does it delete. Use Delete(s1,1, wordcount) instead.

Index[0] in Delphi strings does not contain your character data - it's 'not accessible' according to the compiler, if you try compiling myString[0];

Also: the way your code is written, you MUST terminate with ';' or a string such as this:

s1 := 'mikey;was;here;a'; will loop infinitely on the last string after ';' ('a')

I also use ReportBuilder templates, etc: In Delphi itself you will not be able to compile MyString[0], but the copy and delete methods are protected from this error, (as David explained) however it appears from what I saw in the debugger that 'Delete(s1, 0, wordcount)' will not throw an exception but fails to delete. So I would not expect RBuilder to be any better, and perhaps worse - copy() may also be failing on string[0] in RAP.

RAP is NOT Delphi - it is a Runtime scripting environment that runs in your template, based on Object Pascal, but it does not support everything, and you cannot always expect it to behave exactly like Delphi.

BTW - ReportBuilder is now up to version 14.0X - if possible you should upgrade - there have been a lot of improvements in the RAP environment. In a later version your code might work OK or you'll get back an error message from RAP.

Also: If you want to debug in RAP it's not so easy. But to give you a clue as to where the error might be occurring, put a text label on your report and after each line of your code add

mylabel.caption:='statementxxx ran';

or

mylabel.caption:= myVariable.value;

Etc. That will give you a little ad hoc tracer - maybe show you where/why you failed, etc.

OTHER TIPS

For all searching people: I found the solution with the excellent help of this community!

The working code looks like this:

    procedure DetailBeforeGenerate;
var
  S1: String;
  S2: String;
  wordcount: Integer;
  notelength: Integer;

begin
 S1 := plPrintInvLine['ArtName'];
 notelength := Length(S1);
while (notelength > 0) do
begin
    wordcount := Pos(';',S1);
    S2 :=   Copy(S1, 1, wordcount-1);
    if ( Pos(' ',S2) = 1 ) then Delete(S2, 1, 1);
    Memo1.Lines.Add(S2);
    Delete(S1, 1, wordcount);
    notelength := notelength - wordcount;
end;

end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top