我主要是C ++程序员,我使用Embarcadero C ++ Builder,经常发现我必须用Delphi编写东西。我在程序包库中有一些自定义组件,有些是C ++,有些是Delphi。我的特定问题是关于用Delphi编写的组件。
此组件可正确流式传输到DFM文件,但不包括收集项。收集项在IDE中可见,可以正确设置。
我尝试过包括正在使用C ++编写的另一个组件上工作的集合项。有问题的收集项目是用Delphi编写的。
非工作组件是用Delphi编写的,除此集合外,所有其他属性似乎都可以工作。当包含在另一个组件(用C ++内置)中时,相同的收集类可以正确地进行流传输。
我了解流来自TPersistent类,所有权对于使事物正确流(以及在IDE中工作-确实如此)很重要。
两个TComponent对象都声明了TCollection的已发布属性。在用Delphi编写的对象中,其声明如下:
protected:
{... various other members ...}
function _GetOptions: TITIOOptionChoices; virtual;
published:
{... various other members ...}
property Options: TITIOOptionChoices read _GetOptions;
在用C ++编写的对象中,声明如下:
protected:
virtual TITIOOptionChoices * __fastcall _GetOptions();
virtual void __fastcall _SetOptions(TITIOOptionChoices *pNewValue);
published:
TITIOOptionChoices __property * Options = { read=_GetOptions, write=_SetOptions };
Delphi组件使用以下代码在其create方法中实例化集合:
constructor TITIOFNCUIStyleMatrix.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
{ other property setup code }
Self._pOptions := TITIOOptionChoices.Create(Self);
end;
使用C ++编写的工作组件使用以下代码在AfterConstruction方法中创建集合:
void __fastcall TITIOFNCOptionChooser::AfterConstruction()
{
// other property setup code
this->_pOptions=new TITIOOptionChoices(this);
}
两个组件都继承自TComponent。这两个控件都允许我在IDE中正确设置收集项,但是只有其中一个成功地将收集流式传输到DFM文件。
我无法找到任何原因,为什么我的一个组件确实传输了属性的内容而另一个没有。但是,这表明问题在于如何实例化集合,因为它在一个组件中而不是在另一个组件中工作。
我错过了什么?
答案 0 :(得分:0)
Thanks to the input from the community I have answered the question ...
The Collection property did not have a write accessor specified in the Delphi class.
The IDE did show changes, but (I assume) was not able to write these changes back to the object - and so the object would not stream them.
I had implemented both Assign and AssignTo methods for the collection, I just needed to call the Assign method in the write accessor.
Doh!