4
votes

I read this question in which the same problem is discussed, anyway i was able to do this in Delphi 2009 and this was not possible as i upgraded to XE.

I paste here a simple dummy example: this compiles on 2009 and gives E2064 on XE... Why? Is it possible to setup XE to behave like 2009? Or should I go for a workaround?

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
type
  TTestRecord = record
    FirstItem  : Integer;
    SecondItem  : Integer;
  end;
  TForm2 = class(TForm)
    procedure AssignValues;
  private
    FTestRecord :TTestRecord;
  public
    property TestRecord : TTestRecord read FTestRecord write FTestRecord;
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.AssignValues;
begin
with TestRecord do
     begin
       FirstItem := 14; // this gives error in XE but not in 2009
       SecondItem := 15;
     end;
end;

end.
2
This is a perfect example of why I hate the WITH statement. :-) Even in 2009 when it built, it did something odd, and you would have trouble viewing the values in the debugger.Warren P

2 Answers

12
votes

The D2010 compiler is just more strict than the previous versions were. In the previous versions the compiler didn't complain, but often the results would not be as you would expect as it was acting on a temporary var so your changes would be gone by the end of the method.

The answers on the question to which you linked provide the explanations even better and provide the solutions (or work-arounds) from which to choose.

-1
votes

OK, OK, I am sorry, I shouldn't have made non-technical content...

Now, we can modify the code as follows, and it works fine:

type
  PTestRecord = ^TTestRecord;
  TTestRecord = record
    FirstItem: Integer;
    SecondItem: Integer;
  end;

  TForm2 = class(TForm)
  private
    { Private declarations }
    FTestRecord: TTestRecord;
    procedure AssignValues;
  public
    { Public declarations }
    property TestRecord: TTestRecord read FTestRecord write FTestRecord;
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.AssignValues;
begin
  with PTestRecord(@TestRecord)^ do
  begin
    FirstItem := 14; // it works fine.
    SecondItem := 15;
  end;
end;