3

I'm a beginner to delphi and I'm stuck with a finalization error e2155. I'm using RAD 10 and try to run my programm on mobile devices. It works fine on my windows machine, but when i change to Android or IOS it gives me that finalization error.

The Code:

    type
    TRaumparameter = record
      ID : string;
      Länge: string;
      Breite: string;
      Höhe: string;
      Fläche: string;
      Raumvolumen: string;
      Wände: string;
      Decke: string;
      Boden: string;
      Baujahr: string;
      Heizlast: string;
  end;
  var Aufstellraum: Traumparameter;
    { Public declarations }

  end;

var
  Form1: TForm1;

implementation
{$R *.fmx}
{$R *.iPad.fmx IOS}

procedure TForm1.speichernClick(Sender: TObject);
  var F: File of Traumparameter;
  begin
    Aufstellraum.Länge:=form2.Länge.Text;
    Aufstellraum.Breite:=form2.Breite.Text;
    Aufstellraum.Höhe:=form2.Höhe.Text;
    Aufstellraum.Fläche:=form2.Fläche.Text;
    Aufstellraum.Raumvolumen:=form2.ErgebnisRaumVol.Text;
    Aufstellraum.Wände:=form2.Wände.Text;
    Aufstellraum.Decke:=form2.Decke.Text;
    Aufstellraum.Baujahr:=form2.Baujahr.Selected.Text;
    Aufstellraum.Heizlast:=form2.Heizlast.Text;

    try
      AssignFile(F,'D:\test\1.txt');
      ReWrite(F);
      Write(F,Aufstellraum);
    finally
      CloseFile(F);
    end;
  end;

I already tried to limit the length of the strings with [] but then it tells me: ';' expected but '[' found. Hope I can get some answers because i spent quiet some time without any success. Thanks in advance!!

D. Flo
  • 81
  • 1
  • 8
  • 1
    Legacy Pascal I/O should not be used. Store as JSON. – David Heffernan Jan 02 '16 at 09:20
  • Your code shown here won't work even on windows unless you would be using `ShortString` instead of `String`. Bus as said by LU RD in his answer `ShortStrings` are not supported on Mobile platforms. – SilverWarior Jan 02 '16 at 18:23

2 Answers2

6

When you are trying to write a file of record containing String types, it is not allowed by the compiler:

E2155 Type '%s' needs finalization - not allowed in file type (Delphi)

String is one of those data types which need finalization, and as such they cannot be stored in a File type

It is no point writing a record with a String field using a binary file type anyway, since you will be writing the address instead of the text (string is a reference type).


When you are declaring strings with a dedicated length, they are called ShortString (value type). ShortString is not supported by the mobile compilers though.

I suggest you use other techniques to store text. See how to convert a record to text with json for example.

LU RD
  • 34,438
  • 5
  • 88
  • 296
0

just do it:

TRaumparameter = record
    ID : string[255];
    Länge: string[255];
    Breite: string[255];
    Höhe: string[255];
    Fläche: string[255];
    Raumvolumen: string[255];
    Wände: string[255];
    Decke: string[255];
    Boden: string[255];
    Baujahr: string[255];
    Heizlast: string[255];
 end;
Ivan
  • 1
  • This won't work on mobile targets, as short strings aren't supported there. By using ARRAY[0..255] OF CHAR instead, you can do it (but you'll have to deal with the packing/unpacking from ARRAY OF CHAR to STRING). But other than that, as stated in other replies, you shouldn't use FILE OF in modern programs - use streaming file I/O and/or JSON/XML to store your data. – HeartWare Oct 31 '20 at 08:13