如何更新TList中的数据< T>? [英] How can I update a data in a TList<T>?
问题描述
我有此记录(结构):
type
THexData = record
地址:Cardinal ;
DataLen:Cardinal;
数据:string;
end;
我已声明此列表:
HexDataList:TList< THexData> ;;
我填充了一些数据。现在我想扫描ListHexData,有时更新 HexDataList 中的记录的元素。
有可能吗?我如何做?
var
Item:THexData;
...
for i:= 0 to HexDataList.Count-1 do begin
Item:= HexDataList [i];
//更新项目
HexDataList [i]:= Item;
end;
绑定是您要修改 HexDataList [i] code>到位,但你不能。当我使用保存记录的
TList< T>
时,我实际上是子类 TList< T>
将 Items
属性替换为返回指向该项目的指针的属性,而不是该项目的副本。
再次查看我的代码I意识到我实际上不是子类 TList< T>
,因为该类太私人以提取指向底层数据的指针。这可能是一个很好的决定。我实际上做的是实现我自己的通用列表类,并允许我自由返回指针到记录如果需要。
I have this record (structure):
type
THexData = record
Address : Cardinal;
DataLen : Cardinal;
Data : string;
end;
And I've declared this list:
HexDataList: TList<THexData>;
I've filled the list with some data. Now I'd like scan ListHexData and sometimes update a element of a record inside HexDataList.
Is it possible? How can I do?
var
Item: THexData;
...
for i := 0 to HexDataList.Count-1 do begin
Item := HexDataList[i];
//update Item
HexDataList[i] := Item;
end;
The bind is that you would like to modify HexDataList[i]
in place, but you can't. When I'm working with a TList<T>
that holds records I actually sub-class TList<T>
and replace the Items
property with one that returns a pointer to the item, rather than a copy of the item. That allows for inplace modification.
EDIT
Looking at my code again I realise that I don't actually sub-class TList<T>
because it the class is too private to extract pointers to the underlying data. That's probably a good decision. What I actually do is implement my own generic list class and that allows me the freedom to return pointers to records if needed.
这篇关于如何更新TList中的数据< T>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!