1. Home
  2. Computing & Technology
  3. Delphi Programming

Advanced Record Structure Type in Turbo Delphi and Delphi 2006
Records Almost Like Classes

By , About.com Guide

Advanced Record Type in Turbo Delphi / Delphi 2006

Advanced Record Type in Turbo Delphi / Delphi 2006

Record data types in Delphi represent a mixed set of elements. Each element is called a field; the declaration of a record type specifies a name and type for each field.

Records are commonly used in Microsoft Windows API calls, where they are referred to as "structures".

"Understanding and Using Record Data Types in Delphi" describes what records are, how to declare and use records. What's more, if you know that you will never need to use all of the fields in a single record instance, you can use variant records.

Smarter Records in Turbo Delphi / Delphi 2006

Records in Turbo Delphi (Delphi 2006) are even more powerful!

How about having a record constructor where you can initialize record field values? How about having properties in records? Cool, ha :)

Here's a list of the new record features in Turbo Delphi (Delphi 2006)

  • Records can have constructor, with at least one parameter.
  • Records support properties and static methods.
  • Records support operator overloading.
  • Records can have methods (functions, procedures).
  • Records do NOT support virtual methods ("virtual", "dynamic", and "message")
  • Records do NOT support destructors.
Note: records are value types. Records are are copied on assignment, passed by value.

Here's an example of the new, advanced records in Delphi 2006:

type
  TTurboRecord = record
  strict private
    fNameValue : integer;
    function GetName: string;
  public
    NamePrefix : string;
    constructor Create(const initNameValue : integer) ;
    property Name : string read GetName;
  end;

implementation

constructor TTurboRecord.Create(const initNameValue : integer) ;
begin
  fNameValue := initNameValue;
  NamePrefix := '"record constructor"';
end;

function TTurboRecord.GetName: string;
begin
  Inc(fNameValue) ;
  result := Format('%s %d',[NamePrefix, fNameValue]) ;
end;
Drop a TMemo control on a form and try the following code:
var
  turboRecord : TTurboRecord;
  anotherTurboRecord : TTurboRecord;
begin
  //no Constructor needed - fNameValue = 0 by default
  turboRecord.NamePrefix := 'turboRecord';
  Memo1.Lines.Add(turboRecord.Name) ;
  Memo1.Lines.Add(turboRecord.Name) ;

  //constructor used
  turboRecord := TTurboRecord.Create(2006) ;
  Memo1.Lines.Add(turboRecord.Name) ;
  Memo1.Lines.Add(turboRecord.Name) ;

  anotherTurboRecord := turboRecord;
  anotherTurboRecord.NamePrefix := 'anotherTurboRecord';

  Memo1.Lines.Add(anotherTurboRecord.Name) ;
  Memo1.Lines.Add(anotherTurboRecord.Name) ;

  Memo1.Lines.Add(turboRecord.Name) ;
  Memo1.Lines.Add(turboRecord.Name) ;

  //NO need to FREE record types
end;
Delphi tips navigator:
» Understanding and (Not) Using the Goto Delphi Statement
« Allow Only One Application Execution per Windows Session
More Delphi Programming Quick Tips
Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2006 Tips
  7. Advanced Record Structure Type in Turbo Delphi and Delphi 2006

©2009 About.com, a part of The New York Times Company.

All rights reserved.