Recent

Author Topic: Dynamic array of user-defined records.  (Read 14635 times)

idealis

  • New Member
  • *
  • Posts: 34
Dynamic array of user-defined records.
« on: October 29, 2010, 12:38:06 pm »
Hi again! As you probably noticed I'm opening several topics and they are about rather basic questions, hey, I'm new to Delphi and Lazarus so please bear with me. :D

This time I want to know this: Is it possible to have dynamic arrays of user-defined records? I wrote this code to see if I could get the answer, but the compiler fails.

Code: [Select]
type
  TBanco = Record
             Numero    : Integer;
             Nombre    : String;
             Domicilio : String;
           end;

function ObtenerBancosDBF(var Salida: array of TBanco): Integer;
var
  Limite: Integer;
  DatosOrigen: TDbf;
begin
  Limite := 0;
  // Open DBF
  While not(DatosOrigen.EOF) do begin
    Inc(Limite);
    SetLength(Salida,Limite);
    Salida[Limite-1].Numero := FieldByName('Numero').AsString;
    Salida[Limite-1].Nombre := FieldByName('Nombre').AsString;
    Salida[Limite-1].NombreCorto := FieldByName('Domicilio').AsString;
    DatosOrigen.Next;
  end;
  DatosOrigen.Close;
end;

This specific line gives me a "type mismatch" error.

Code: [Select]
SetLength(Salida,Limite);
But this is supposed to work, according to some stuff that I read on the Internet. What is wrong here?

Thanks :)

- idealis
« Last Edit: October 29, 2010, 12:44:39 pm by idealis »

eny

  • Hero Member
  • *****
  • Posts: 1662
Re: Dynamic array of user-defined records.
« Reply #1 on: October 29, 2010, 01:08:26 pm »
Try:
Code: Pascal  [Select][+][-]
  1. type
  2.   TBanco = Record
  3.              Numero    : Integer;
  4.              Nombre    : String;
  5.              Domicilio : String;
  6.            end;
  7.   TBancoArray = array of TBanco;
  8.  
  9. function ObtenerBancosDBF(var Salida: TBancoArray): Integer;
  10. //etc...
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12209
  • Debugger - SynEdit - and more
    • wiki
Re: Dynamic array of user-defined records.
« Reply #2 on: October 29, 2010, 01:19:49 pm »
Code: [Select]
function ObtenerBancosDBF(var Salida: array of TBanco): Integer;

What you have there is an "open array" http://www.freepascal.org/docs-html/ref/refsu57.html#x137-14400011.4.5

which differs from a dynamic array  http://www.freepascal.org/docs-html/ref/refsu14.html#x38-440003.3.1


idealis

  • New Member
  • *
  • Posts: 34
Re: Dynamic array of user-defined records.
« Reply #3 on: November 01, 2010, 05:09:04 pm »
Thank you so much for the replies, friends.
Also, Martin_fr, those links were very useful, I will read more about it when I have some time. :)

Cheers!

- idealis

 

TinyPortal © 2005-2018