【问题标题】:Delphi "E2064 Left side cannot be assigned to" error appeared when upgrading a project from 2009 to XE将项目从 2009 升级到 XE 时出现 Delphi "E2064 Left side cannot be assigned to" 错误
【发布时间】:2011-03-30 08:57:27
【问题描述】:

我阅读了this question in which the same problem is discussed,无论如何我可以在 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.

【问题讨论】:

  • 这是我讨厌 WITH 语句的一个完美例子。 :-) 即使在 2009 年构建时,它也做了一些奇怪的事情,您在调试器中查看值时会遇到问题。

标签: delphi compiler-errors delphi-2009 delphi-xe


【解决方案1】:

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

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

【讨论】:

  • 换句话说,感谢 XE 编译器不再让你自取其辱。 +1。
  • 可能是的。添加此属性是最近的更改,未经测试。
  • 如果您希望能够直接修改字段,请将属性设为 PTestRecord = ^TTestRecord;并使用返回该指针的 Get 函数和执行 FTestRecord := aValue^; 的 Set 函数
【解决方案2】:

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

现在,我们可以修改代码如下,效果很好:

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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-28
    • 2012-05-31
    • 1970-01-01
    • 2021-05-13
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多