Created
October 14, 2021 11:03
-
-
Save mrandreastoth/692f00077dfcf7fedf3bc09197133e92 to your computer and use it in GitHub Desktop.
Delphi record usage test (tested under Delphi 2007 and 10.4.2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit UTestRecord; | |
// By Andreas Toth | |
// | |
// NOTE: The greatest mistake when using $IF is to use $ENDIF instead of $IFEND | |
// when writing code meant to be backwards compatible as $ENDIF is not | |
// supported by older Delphi versions, in particular not Delphi 2007! | |
// Similary, $IFDEF must be terminated by $ENDIF! | |
//{$DEFINE INCLUDE_ERROR_E2064_LEFT_SIDE} | |
//{$DEFINE INCLUDE_ERROR_E2129_READ_ONLY} | |
interface | |
type | |
TTestRecord = record | |
private | |
procedure SetField(const AValue: Integer); | |
public | |
FField: Integer; // Public to allow testing | |
property PropertyField1: Integer read FField; | |
property PropertyField2: Integer read FField write FField; | |
property PropertyField3: Integer read FField write SetField; | |
end; | |
// PTestRecord = ^TTestRecord; | |
TTestRecordObject = class(TObject) | |
private | |
procedure SetRecord(const AValue: TTestRecord); | |
public | |
FRecord: TTestRecord; // Public to allow testing | |
property PropertyRecord1: TTestRecord read FRecord; | |
property PropertyRecord2: TTestRecord read FRecord write FRecord; | |
property PropertyRecord3: TTestRecord read FRecord write SetRecord; | |
// private | |
// procedure SetPRecord(const AValue: PTestRecord); | |
// public | |
// FPRecord: PTestRecord; // Public to allow testing | |
// | |
// property PropertyPRecord1: PTestRecord read FPRecord; | |
// property PropertyPRecord2: PTestRecord read FPRecord write FPRecord; | |
// property PropertyPRecord3: PTestRecord read FPRecord write SetPRecord; | |
end; | |
procedure TestRecord; | |
implementation | |
uses | |
SysUtils; | |
procedure TestRecord; | |
var | |
LSource: TTestRecordObject; | |
LRecord: TTestRecord; | |
LObject: TTestRecordObject; | |
begin | |
LSource := TTestRecordObject.Create; | |
try | |
LObject := TTestRecordObject.Create; | |
try | |
/////////////////////////////////////////////////////////////////////////// | |
LSource.FRecord.FField := 10; | |
LRecord := LSource.FRecord; | |
/////////////////////////////////////////////////////////////////////////// | |
LSource.FRecord.FField := 2000; | |
LRecord.FField := LSource.FRecord.FField; | |
LSource.FRecord.FField := 2001; | |
LRecord.FField := LSource.FRecord.PropertyField1; | |
LSource.FRecord.FField := 2002; | |
LRecord.FField := LSource.FRecord.PropertyField2; | |
LSource.FRecord.FField := 2003; | |
LRecord.FField := LSource.FRecord.PropertyField3; | |
/////////////////////////////////////////////////////////////////////////// | |
LSource.FRecord.FField := 3000; // Same as test 2000 | |
LRecord.FField := LSource.FRecord.FField; | |
{$IFDEF INCLUDE_ERROR_E2129_READ_ONLY} // Delphi 2007 & 10.4.2 | |
LSource.FRecord.FField := 3010; | |
LRecord.PropertyField1 := LSource.FRecord.FField; | |
{$ENDIF} | |
LSource.FRecord.FField := 3020; | |
LRecord.PropertyField2 := LSource.FRecord.FField; | |
LSource.FRecord.FField := 3030; | |
LRecord.PropertyField3 := LSource.FRecord.FField; | |
/////////////////////////////////////////////////////////////////////////// | |
LSource.FRecord.FField := 400000; | |
LObject.FRecord.FField := LSource.FRecord.FField; | |
LSource.FRecord.FField := 400001; | |
LObject.FRecord.FField := LSource.FRecord.PropertyField1; | |
LSource.FRecord.FField := 400002; | |
LObject.FRecord.FField := LSource.FRecord.PropertyField2; | |
LSource.FRecord.FField := 400003; | |
LObject.FRecord.FField := LSource.FRecord.PropertyField3; | |
/////////////////////////////////////////////////////////////////////////// | |
LSource.FRecord.FField := 500000; // Same as test 400000 | |
LObject.FRecord := LSource.FRecord; | |
{$IFDEF INCLUDE_ERROR_E2129_READ_ONLY} // Delphi 2007 & 10.4.2 | |
LSource.FRecord.FField := 500100; | |
LObject.FRecord.PropertyField1 := LSource.FRecord.FField; | |
{$ENDIF} | |
LSource.FRecord.FField := 500200; | |
LObject.FRecord.PropertyField2 := LSource.FRecord.FField; | |
LSource.FRecord.FField := 500300; | |
LObject.FRecord.PropertyField3 := LSource.FRecord.FField; | |
/////////////////////////////////////////////////////////////////////////// | |
LSource.FRecord.FField := 600000; // Same as test 400000 | |
LObject.FRecord.FField := LSource.FRecord.FField; | |
{$IFDEF INCLUDE_ERROR_E2129_READ_ONLY} // Delphi 2007 & 10.4.2 | |
LSource.FRecord.FField := 601000; | |
LObject.PropertyRecord1 := LSource.FRecord; | |
{$ENDIF} | |
LSource.FRecord.FField := 602000; | |
LObject.PropertyRecord2 := LSource.FRecord; | |
LSource.FRecord.FField := 603000; | |
LObject.PropertyRecord3 := LSource.FRecord; | |
/////////////////////////////////////////////////////////////////////////// | |
LSource.FRecord.FField := 700000; // Same as test 400000 | |
LObject.FRecord := LSource.FRecord; | |
{$IFDEF INCLUDE_ERROR_E2064_LEFT_SIDE} // Delphi 2007 & 10.4.2 | |
LSource.FRecord.FField := 701000; | |
LObject.PropertyRecord1.FField := LSource.FRecord.FField; | |
{$ENDIF} | |
{$IFDEF INCLUDE_ERROR_E2129_READ_ONLY} // Delphi 2007 & 10.4.2 | |
LSource.FRecord.FField := 701100; | |
LObject.PropertyRecord1.PropertyField1 := LSource.FRecord.FField; | |
{$ENDIF} | |
{$IFDEF INCLUDE_ERROR_E2064_LEFT_SIDE} // Delphi 2007 & 10.4.2 | |
LSource.FRecord.FField := 701200; | |
LObject.PropertyRecord1.PropertyField2 := LSource.FRecord.FField; | |
{$ENDIF} | |
LSource.FRecord.FField := 701300; | |
LObject.PropertyRecord1.PropertyField3 := LSource.FRecord.FField; | |
/////////////////////////////////////////////////////////////////////////// | |
LSource.FRecord.FField := 800000; // Same as test 400000 | |
LObject.FRecord := LSource.FRecord; | |
{$IFDEF INCLUDE_ERROR_E2064_LEFT_SIDE} // Delphi 2007 & 10.4.2 | |
LSource.FRecord.FField := 802000; | |
LObject.PropertyRecord2.FField := LSource.FRecord.FField; | |
{$ENDIF} | |
{$IFDEF INCLUDE_ERROR_E2129_READ_ONLY} // Delphi 2007 & 10.4.2 | |
LSource.FRecord.FField := 802100; | |
LObject.PropertyRecord2.PropertyField1 := LSource.FRecord.FField; | |
{$ENDIF} | |
{$IFDEF INCLUDE_ERROR_E2064_LEFT_SIDE} // Delphi 2007 & 10.4.2 | |
LSource.FRecord.FField := 802200; | |
LObject.PropertyRecord2.PropertyField2 := LSource.FRecord.FField; | |
{$ENDIF} | |
LSource.FRecord.FField := 802300; | |
LObject.PropertyRecord2.PropertyField3 := LSource.FRecord.FField; | |
/////////////////////////////////////////////////////////////////////////// | |
LSource.FRecord.FField := 900000; // Same as test 400000 | |
LObject.FRecord := LSource.FRecord; | |
{$IFDEF INCLUDE_ERROR_E2064_LEFT_SIDE} // Delphi 2007 & 10.4.2 | |
LSource.FRecord.FField := 903000; | |
LObject.PropertyRecord3.FField := LSource.FRecord.FField; | |
{$ENDIF} | |
{$IFDEF INCLUDE_ERROR_E2129_READ_ONLY} // Delphi 2007 & 10.4.2 | |
LSource.FRecord.FField := 903100; | |
LObject.PropertyRecord3.PropertyField1 := LSource.FRecord.FField; | |
{$ENDIF} | |
{$IFDEF INCLUDE_ERROR_E2064_LEFT_SIDE} // Delphi 2007 & 10.4.2 | |
LSource.FRecord.FField := 903200; | |
LObject.PropertyRecord3.PropertyField2 := LSource.FRecord.FField; | |
{$ENDIF} | |
LSource.FRecord.FField := 903300; | |
LObject.PropertyRecord3.PropertyField3 := LSource.FRecord.FField; | |
/////////////////////////////////////////////////////////////////////////// | |
finally | |
FreeAndNil(LObject); | |
end; | |
finally | |
FreeAndNil(LSource); | |
end; | |
end; | |
{ TTestRecord } | |
procedure TTestRecord.SetField(const AValue: Integer); | |
begin | |
FField := AValue; | |
end; | |
{ TTestRecordObject } | |
procedure TTestRecordObject.SetRecord(const AValue: TTestRecord); | |
begin | |
FRecord := AValue; | |
end; | |
//procedure TTestRecordObject.SetPRecord(const AValue: PTestRecord); | |
//begin | |
// FPRecord := AValue; | |
//end; | |
initialization | |
TestRecord; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment