-3

I have a Form. When the user clicks the TESTBUTON an array is generated (here with a loop) and an array is filled. (that works). Now the user will be able to change some parameters an hit the button again. Than I want to clear / free / destroy the old array an create it new.

I found a lot of examlpes for that but they not work (because I do not know where exatly to place the different procedures). So I made this samplescript with all the sections. Can someone move my procedures to the rigth place or send me an example that shows the correct implementation.

unit frmmywindow;

interface

uses 
type 

  TArrayA = record
        Field1:integer;
        Field2:integer;
        Field3:integer;
        Field4:integer;
        String5:string;
         //other fields, strings, integers..
      end; 

  private
    { private declarations }

  public
    { public declarations }
    destructor Destroy; override;
  end;

var
  var ArrayA : array of TarrayA;

implementation 

destructor TArrayA.Destroy;
begin
  ArrayA.Free;
  inherited;
end; 

procedure TArrayA.Free;
begin
     if Assigned(self) then Destroy;
end;     

procedure TForm1.btnTest(Sender: TObject);
var
  x: integer;
  reccount: integer;
begin
  ArrayA.free:
  ArrayA.create;
  reccount := 1000;
  for x := 1 to reccount do
  begin
        setLength(ArrayA,x+1); 
         ArrayA[x].field1 := 2000 - x;
         ArrayA[x].field2 := x;
         ArrayA[x].field3 := x;
         ArrayA[x].field4 := x;
         ArrayA[x].string5 := 'str' + inttostr(x);
  end;    
end;
ratmalwer
  • 700
  • 5
  • 14
  • 6
    Why not read a decent Pascal/Delphi tutorial? – Andreas Rejbrand Nov 12 '17 at 22:43
  • `array of TArrayA` is dynamic array. You can add/remove its memory using SetLength(). Try search engine with keyword `how to remove dynamic array` – Zamrony P. Juhara Nov 13 '17 at 00:31
  • 3
    `TArrayA` is a **`record`**. They do not have destructors. Only objects of type `class` (i.e., derived from `TObject`) have destructors. And you should not code `Free` at all, not even for objects. It is inherited from `TObject`. You should, as @Andreas said, really, really read a proper tutorial on FreePascal before you start guessing. The above is pure guesswork. – Rudy Velthuis Nov 13 '17 at 07:02
  • FWIW, the fact that you think of the source code above as a "script" is quite telling. It is a compiled language. Do not confuse those. – Rudy Velthuis Nov 13 '17 at 07:06
  • thanks Zamrony and Rudy for your helpful comments – ratmalwer Nov 13 '17 at 18:14

1 Answers1

0

Your code has a number of issues.

  • The main issue is that TArrayA is a record (normally I would link to the DocWiki documentation for structured types, but it seems to be down right now). Records are not classes, they don't have a destructor and you should not call Free on them. Records are so called value types. They don't even have a proper constructor, even if the syntax suggests they do. Record "constructors" are mere initializers.
  • Another issue is that you should never code Free yourself, not even for classes. Free is inherited from the root for all class instances, TObject. For classes, if you want to give it a destructor, override the inherited destructor:

    destructor Destroy; override;
    

So the answer is: you don't use nor define them at all, for records.

How you should declare, define and use them for classes is described in the documentation.


As I already commented, you should get better acquainted with the language. I suggest you read the Delphi or Object Pascal Language Guide (name differs, depending on version), which is part of the documentation that is installed with Delphi.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94