德尔福:问题设置一个TByteDynArray的长度 [英] Delphi: problem setting length of a TByteDynArray

查看:59
本文介绍了德尔福:问题设置一个TByteDynArray的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已关闭问题(接受了答案),已由 Delphi 7:访问冲突-TByteDynArray问题)

Question closed (answer accepted) replaced by Delphi 7: Access violation - TByteDynArray problem)

我得到了以下指示

MyPhoto = class(TRemotable)
private
  FhasPhoto: Boolean;
  FphotoData: TByteDynArray;
published
  property hasPhoto: Boolean read FhasPhoto write FhasPhoto;
  property photoData: TByteDynArray read FphotoData write FphotoData;
end;

我想

var photo : MyPhoto;
photo := MyPhoto.Create();
SetLength(photo.photoData, 4);

但我知道

[Error] mainForm.pas(340): Constant object cannot be passed as var parameter

1)鉴于我无法更改Photo类的定义,我该如何正确编码?
2)是否可以仅通过分配将结构有效地投射"到TByteDynArray?

1) How do I code it correctly, given that I can't change the definition of the class Photo?
2) can I effectively 'cast' any structure to a TByteDynArray just by assigning it?

(您可能已经猜到了,我是一个试图加入Ddlphi的BCB家伙:-)

(as you might have guessed, I'm a BCB guy trying to get into Ddlphi :-)

p.s我会满足于能够分别分配照片数据的每个字节...

p.s I will settle for being able to assign each byte of the photo data individually...

推荐答案

这是Delphi实现属性的一个缺陷.即使可以读取和写入属性,即使读取和写入都指向基础对象中的同一字段,也无法将属性传递给 var 参数,例如到SetLength.没有充分的理由,但这至少是暂时的.

This is a flaw in Delphi's implementation of properties. Even if the property can be read and written to, even if both the read and the write point to the same field in the underlying object, you can't pass a property to a var parameter, such as to SetLength. There's no good reason for this, but that's the way it works, for the time being at least.

但是,对于此问题,有一个相当简单的解决方法.您需要在该对象上采用一个整数的方法,该方法将在内部设置FPhotoData的长度.

There's a fairly simple workaround for this issue, though. You need a method on the object that takes an integer and will set the length of FPhotoData internally.

这篇关于德尔福:问题设置一个TByteDynArray的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆