On Fri, 28 May 2010, spir ☣ wrote:
Hello, While waiting for more information about how to use "associated objects" with TStringList, I started to implement // lists for names (TString) and values (TFPList). This builds a kind of flexible record type, I called "Struct" because the name "Record" is not available ;-) Values are in in fact Struct-s themselves. All works fine when storing data, but I cannot get it back. Precisely, i := self.names.indexOf(name); val := Struct(self.values[i]); raises "got untyped expected Struct". Sure, that's why I'm trying to cast... Without casting to Struct, the compiler indeed throws "got pointer expected Struct". I'm very surprised since I already did this. So, I quickly wrote a simpler test case wrapping structure, just putting objects in a TFPList and casting back: all works fine:
If you want this, I suggest you look at fpjson. it does all this for you. It has an extensive testsuite, so plenty of code to peep at. Michael.
======================== program __essai__; {$mode objfpc}{$H+} uses Classes, SysUtils; type Struct = Class val : Integer; constructor struct(i:Integer); function text : String; end; constructor Struct.struct(i:Integer); begin inherited; self.val := i; end; function Struct.text : String; begin result := '<' +intToStr(self.val)+ '>'; end; var s : Struct; l : TFPList; begin s := Struct.struct(1); writeln(s.text); l := TFPList.create; l.add(s); s := Struct(l[0]); ////////////////// writeln(s.text); end. === output === <1> <1> Thank you, Denis ________________________________ vit esse estrany ☣ spir.wikidot.com _______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
_______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal