Delphi“E2064 左侧无法分配给”将项目从2009升级到XE时出现错误

发布于 2024-10-27 22:04:07 字数 945 浏览 4 评论 0原文

我读过 这个问题,其中相同问题已讨论,无论如何,我能够在 Delphi 2009 中做到这一点,但当我升级到 XE 时,这是不可能的。

我在这里粘贴一个简单的虚拟示例:它在 2009 年编译并在 XE 上给出 E2064...为什么?是否可以将 XE 设置为像 2009 一样?或者我应该寻求解决方法?

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烙印 2024-11-03 22:04:07

D2010 编译器只是比以前的版本更严格。在以前的版本中,编译器不会抱怨,但结果通常不会像您预期的那样,因为它作用于临时变量,因此您的更改将在方法结束时消失。

您链接到的问题的答案提供了更好的解释,并提供了可供选择的解决方案(或解决方法)。

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.

々眼睛长脚气 2024-11-03 22:04:07

好吧好吧,对不起,我不应该做出非技术性的内容……

现在,我们可以修改代码如下,就可以正常工作了:

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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文