自己创建了两个简单的类,其中tc2包含了列表,用于存放tc1的对象:
  tc1=class(tcomponent)
   public
   i,j:integer;
   str:string;
  end;
  tc2=class(tcomponent)
  public
    list:tlist;
    test:string;
    constructor create;
  end;
然后创建一个tc2对象实例,并写入一个文件中。
var
  stream:tmemorystream;
  count1:integer;
  c1:tc1;
  c2:tc2;
begin
c2:=tc2.create;
for count1:=0 to 4 do
  begin
  c1:=tc1.Create(nil);
  c1.i:=count1;
  c1.j:=count1+2;
  c1.str:=inttostr(count1);
  c2.list.add(c1);
  end;
 showmessage(tc1(c2.list[3]).str);
c2.test:='leowangyu';
stream:=tmemorystream.Create;
stream.Writecomponent(c2);
stream.SaveToFile('1.txt');
end; 
这时,该文件只有11字节。好像并没有将tc2的实例保存下来。
试着恢复一下这个对象。
var
  c2:tc2;
  stream:tmemorystream;
begin
stream:=tmemorystream.Create;
stream.LoadFromFile('1.txt');
stream.Position:=0;
end;
结果在stream.ReadComponent(c2);执行时报错。
本来我也有心理准备,可能tc2中无法恢复list的值,但是至少应该将对象创建出来吧。
很郁闷,
请高手指点!
多谢!!

解决方案 »

  1.   

    我来说说看是什么原因把.
    第一.因为你要保存什么东西都应该告诉系统是多大的一个东西.
    所以你不能用string要用string[n](string[255]);
    第二.你要保存list就要一个一个的保存,这样sizeof才能起做用.归为一句就是你有的保存的是地址,不是内容,所以恢复时候内容不一样.
    如果保存的地址被其他程序占用,还会报错.
      

  2.   

    WriteComponent写入文件的只是控件的Published的属性,不会保存变量的