升级Delphi后,为什么TRect的"左侧无法分配"?

Eur*_*n R 1 delphi compiler-errors delphi-7 delphi-xe2

我正在将代码从Delphi 7迁移到XE2之一的Graphical模块.我们正在使用TRect变量,旧代码在Delphi 7中工作没有问题

例如:

Var
  Beold : TRect
begin
  Beold.left := Beold.right;
end.
Run Code Online (Sandbox Code Playgroud)

在将代码移植到新XE2时,我们面临问题E0264:无法分配左侧

能否解释一下XE2 TRect和D7的变化,我们如何分配valuse

Ken*_*ite 9

您发布的代码在快速Delphi测试应用程序中编译并运行良好,因此它不是您真正的代码.

但是,我怀疑你所遇到的是with与使用属性相关的声明中的变化.以前版本的Delphi中存在一个存在多年的错误,最近已经修复了.IIRC,它首先在DME10的README.HTML文件的注释中提到.它已被添加到XE2中的文档中(不是作为行为更改,但记录了新行为).该文档位于docwiki.

(附加信息:它一定是2010年它发生了变化;MarcoCantù Delphi 2010 Handbook在第111页提到它是"With With Statement Now Preserves Read-Only Properties",它描述了这种行为以及我在下面指出的解决方案.)

with您现在需要声明一个局部变量,而不是直接使用语句来访问类的属性,而是直接读取和写入整个事物(为了清楚起见,省略了错误处理 - 是的,我知道应该有一个try..finally块释放位图).

var
  R: TRect;
  Bmp: TBitmap;

begin
  Bmp := TBitmap.Create;
  Bmp.Width := 100;
  Bmp.Height := 100;
  R := Bmp.Canvas.ClipRect;
  { This block will not compile, with the `Left side cannot be assigned to` error
  with Bmp.Canvas.ClipRect do
  begin
    Left := 100;
    Right := 100;
  end;
  }
  // The next block compiles fine, because of the local variable being used instead
  R := Bmp.Canvas.ClipRect;
  with R do
  begin
    Left := 100;
    Right := 100;
  end;
  Bmp.Canvas.ClipRect := R;
  // Do other stuff with bitmap, and free it when you're done.
end.
Run Code Online (Sandbox Code Playgroud)

  • +1从问题中的代码开始,这是一个非常合理的跳跃. (2认同)