English 中文(简体)
如何修改TList<record>价值?
原标题:How to modify TList<record> value?

Delphi 2010 How to modify TList < record > value ?

type TTest = record a,b,c:Integer end;
var List:TList<TTest>;
    A:TTest;
    P:Pointer;
....
....

List[10] := A;  <- OK
List[10].a:=1;  <- Here compiler error : Left side cannot be assined to
P:=@List[10];   <- Error: Variable requied
问题回答
A := List[10];
A.a := 1;
list[10] := A;

你不需要用物体做这件事,因为它们重复了参考类型(通过汇编者在内部管理的一个要点来避免其理发),但记录是价值类型,因此没有工作。

你们用记录击中了一个天线。

考虑这一法典:

function Test: TTest;
begin
    ...
end;

Test.a := 1;

你们的法典实际上认为:

TTest temp := Test;
temp.a := 1;

汇编者用错误信息告诉你,这一转让毫无意义,因为它只会给临时记录价值带来新的价值,而这种价值将立即被遗忘。

此外,<代码>@List[10]是无效的,因为<代码>List[10]<>> 代码再次只收回临时记录价值,因此该记录的地址是毫无意义的。

然而,阅读和书写整个记录是大韩民国。

总结:

List[10] := A;  <- writing a whole record is OK
List[10].a:=1;  <- List[10] returns a temporary record, pointless assignment
P:=@List[10];   <- List[10] returns a temporary record, its address is pointless

如果你想存储记录,动态阵列更适合处理:

type TTest = record a,b,c : Integer end;
type TTestList = array of TTest;
var List:TTestList;
    A:TTest;
    P:Pointer;
....
....

SetLength( List, 20 );
List[10]   := A; //<- OK
List[10].a := 1; //<- Ok
P := @List[10];  //<- Not advised (the next SetLength(List,xx) will blow the address away),
                 //   but technically works

如果你需要增加操纵这些数据的方法,那么你可以把这种阵列作为一类的领域,并将你的方法添加到这一类。

如果你需要以这种形式操纵物体,那么最好使用物体而不是TList,并将结构定义为一个类别而不是一个记录:

type TTest = class a,b,c:Integer end;
var List:TObjectList<TTest>;
    A:TTest; // A is an object so there s no need for a pointer
....
....
List.Add(TTest.Create);
List.Last.a := 1;
A:=List.Last;




相关问题
determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

Help with strange Delphi 5 IDE problems

Ok, I m going nuts here. For the last (almost) four years, I ve been putting up with some extremely bad behavior from my Delphi 5 IDE. Problems include: Seemingly random errors in coride50.bpl ...

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How convert string to integer in Oxygene

In Delphi, there is a function StrToInt() that converts a string to an integer value; there is also IntToStr(), which does the reverse. These functions doesn t appear to be part of Oxygene, and I can ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签