How to modify TList value?

前端 未结 4 746
醉酒成梦
醉酒成梦 2020-12-16 17:33

Delphi 2010 How to modify TList < record > value ?

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


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-16 17:35

    You've hit upon a snag with using records.

    Consider this code:

    function Test: TTest;
    begin
        ...
    end;
    
    Test.a := 1;
    

    What your code looks like to the compiler is actually this:

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

    The compiler is telling you, with the error message, that the assignment is pointless, since it will only assign a new value to a temporary record value, which will be instantly forgotten.

    Also, the @List[10] is invalid because List[10] again returns only a temporary record value, so taking the address of that record is rather pointless.

    However, reading and writing the whole record is OK.

    So to summarize:

    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
    

提交回复
热议问题