#1 2018-03-27 20:28:25

mauriciobs
Member
From: Brazil
Registered: 2016-05-11
Posts: 16

How do I create a dynamic array in a property?

TTelefone = class(TSynAutoCreateFields)
protected
  fNumero: RawUTF8;
published
  property Numero: RawUTF8 read fNumero write fNumero;
end;
TTelefones = array of TTelefone;

TContato = class(TSynAutoCreateFields)
protected
  fNome: RawUTF8;
  fTelefones: TTelefones;
published
  property Nome: RawUTF8 read fNome write fNome;
  property Telefones: TTelefones read fTelefones write fTelefones;
end;
TContatos = array of TContato;

TSQLEmpresas = class(TSQLRecord)
protected
  fNome: RawUTF8;
  fContatos: TContatos;
public
  class procedure InitializeTable(Server: TSQLRestServer; const FieldName: RawUTF8;
    Options: TSQLInitializeTableOptions); override;
published
  property Nome: RawUTF8 index 80 read fnome write fnome;
  property Contatos: TContatos read fContatos write fContatos;
end;
class procedure TSQLEmpresas.InitializeTable(Server: TSQLRestServer;
  const FieldName: RawUTF8; Options: TSQLInitializeTableOptions);
var
  SQLEmpresa: TSQLEmpresas;
  Contato: TContato;
begin
  inherited InitializeTable(Server, FieldName, Options);
  if (FieldName = '') then
  begin
    SQLEmpresa := TSQLEmpresas.Create;
    try
      SQLEmpresa.Nome := 'TESTE 1';
      With SQLEmpresa.DynArray('Contatos') do begin
        Contato      := TContato.Create;
        Contato.Nome := 'EMPRESA 1';

        Contato.Telefones  <<<<<------------------ How to create dynamic array here?

        Add(Contato);
      end;
      Server.Add(SQLEmpresa, true);
    finally
      SQLEmpresa.Free;
    end;
  end;
end;

Offline

#2 2018-03-27 21:40:19

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: How do I create a dynamic array in a property?

Please don't post such big amount of code in the forum directly, but use a pastebin external website.
Check again the forum rules at https://synopse.info/forum/misc.php?action=rules

For your question, the idea is to use a local dynamic array variable, fill it, then assign it to Contato.Telefones.
Using local variables is much better than using "with" or the "DynArray()" method.

And since they will be copied by reference, using a reference count, it will be instant and won't use any more memory or CPU.

Offline

#3 2018-03-28 12:52:08

mauriciobs
Member
From: Brazil
Registered: 2016-05-11
Posts: 16

Re: How do I create a dynamic array in a property?

Sorry for the code posting directly. Thanks for the quick response, it worked using local dynamic arrays.

Offline

Board footer

Powered by FluxBB