How to set default value for writing in delphi

I am using RAD XE7. In my Delphi application, I want to set default values ​​for Records fields.

I tried the following code, but it does not compile, I know this is wrong. Am I there a different way?

 TDtcData = record
    TableFormat     : TExtTableFormat = fmNoExtendedData;
    DTC             : integer = 0;
    Description     : string = 'Dummy';
    Status          : TDtcStatus;    
    OccurenceCnt    : integer =20;
    FirstDTCSnapShot: integer;
    LastDTCSnapShot: integer;
  end; 
+1
source share
2 answers

If you want to define a partially initialized record, simply declare a permanent record , but omit those parameters that do not need default values:

Type
  TDtcData = record
  TableFormat     : TExtTableFormat;
  DTC             : integer;
  Description     : string;
  Status          : TDtcStatus;
  OccurenceCnt    : integer;
  FirstDTCSnapShot: integer;
  LastDTCSnapShot: integer;
end;

Const
  cDefaultDtcData : TDtcData = 
    (TableFormat : fmNoExtendedData; 
     DTC : 0; 
     Description : 'Dummy'; 
     OccurenceCnt : 20);

var
  someDtcData : TDtcData;
begin
  ...
  someDtcData := cDefaultDtcData;
  ...
end;
+5
source

With the addition of a type class such as ' in Delphi, you can solve this using the class function.

Define class function CreateNew: TDtcData; static;for your entry.

:

class function TDtcData.CreateNew: TDtcData;
begin
 Result.TableFormat := fmNoExtendedData;
 Result.DTC := 0;
 Result.Description :=  'Dummy';
 Result.OccurenceCnt := 20;
end;

, , :

var
  AData: TDtcData;
begin
  AData := TDtcData.CreateNew;;
end.
+3

Source: https://habr.com/ru/post/1693409/


All Articles