1. Computing & Technology

Discuss in my forum

How to Declare a Constant Record in Delphi

By , About.com Guide

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.

Constant Record

To create a constant or "read-only" record, use the next "initialization":
 //record declaration
 type
   TDeveloper = record
     Language : string;
     Experience : integer;
     Salary : Currency;
   end;
 
 ... 
 //"DelphiDeveloper" TDeveloper constant
 const
   DelphiDeveloper : TDeveloper =
       (
         Language : 'Delphi';
         Experience : 15;
         Salary : 200000
       ) ;
 
Note that if you try to assign a different value to any of the fields of the constant "DelphiDeveloper", you will get a compile time exception:
 begin
   //compile time exception:
   "Left side cannot be assigned to"
   DelphiDeveloper.Language := 'C#'
 end;
 

Delphi tips navigator:
» How to Override Delphi Form's Restore Operation?
« How to Store a String value to a Tag Property of a Delphi control

©2012 About.com. All rights reserved.

A part of The New York Times Company.