M
MengZhaoXing
Unregistered / Unconfirmed
GUEST, unregistred user!
TStrings 中存储 object
我在 TStrings 中存储子定义object时 addobjects('anystring',TObject(myobject))
出错,"abstract error", 谁能帮我?
源吗如下:
////////////////////////////////
unit main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Edit2: TEdit;
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TFlow = class(TComponent)
private
procedure ReadFlow(Reader: TReader);
procedure WriteFlow(Writer: TWriter);
public
Name: String;
list: TStrings;
constructor create(component: TComponent)
override;
destructor destroy
override;
protected
procedure DefineProperties(Filer: TFiler)
override;
end;
PTempList = ^TTempList;
TTempList = class(TComponent)
private
procedure ReadTemp(Reader: TReader);
procedure WriteTemp(Writer: TWriter);
public
Name: String;
constructor create(component: TComponent)
override;
destructor destroy
override;
protected
procedure DefineProperties(Filer: TFiler)
override;
end;
var
Form1: TForm1;
Flow: TFlow;
implementation
{$R *.DFM}
constructor TTempList.create(component: TComponent);
begin
inherited Create(component);
name := '';
end;
destructor TTempList.destroy;
begin
inherited destroy;
end;
procedure TTempList.DefineProperties(Filer: TFiler);
begin
inherited DefineProperties(Filer);
Filer.DefineProperty('TempInfo',ReadTemp, WriteTemp, true);
end;
procedure TTempList.ReadTemp(Reader: TReader);
begin
Name := Reader.ReadString;
end;
procedure TTempList.WriteTemp(Writer: TWriter);
begin
Writer.WriteString(Name);
end;
constructor TFlow.create(component: TComponent);
begin
inherited create(component);
List := TStrings.Create;
end;
destructor TFlow.destroy;
begin
List.Free;
inherited destroy;
end;
procedure TFlow.DefineProperties(Filer: TFiler);
begin
inherited DefineProperties(Filer);
Filer.DefineProperty('FlowInfo',ReadFlow, WriteFlow, true);
end;
procedure TFlow.ReadFlow(Reader: TReader);
begin
Name := Reader.ReadString;
Reader.Read(list,sizeOf(List));
end;
procedure TFlow.WriteFlow(Writer: TWriter);
begin
Writer.WriteString(name);
Writer.Write(List,SizeOf(List));
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Flow := TFlow.Create(self);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
FileStream: TFileStream;
temp: TTempList;
begin
try
temp := TTempList.Create(self);
temp.Name := Edit2.Text;
Flow.Name := Edit1.Text;
------------------------------------------------------------
Flow.list.AddObject('tmplist',TObject(temp));
--------- ??????运行时出错,'abstract error'------------------
FileStream := TFileStream.Create('c:/flow.dat',fmCreate);
FileStream.WriteComponent(Flow);
finally
FileStream.Free;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
FileStream: TFileStream;
temp: PTempList;
begin
try
new(temp);
Temp^ := TTempList.Create(self);
Flow.destroy;
Flow := TFlow.Create(self);
Flow.Name := '';
FileStream := TFileStream.Create('c:/flow.dat',fmOpenRead);
FileStream.ReadComponent(Flow);
temp^ := TTempList(Flow.list.objects[0]);
Edit2.Text := Temp^.Name;
Edit1.Text := Flow.Name;
finally
FileStream.Free;
dispose(temp);
end;
end;
end.
我在 TStrings 中存储子定义object时 addobjects('anystring',TObject(myobject))
出错,"abstract error", 谁能帮我?
源吗如下:
////////////////////////////////
unit main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Edit2: TEdit;
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TFlow = class(TComponent)
private
procedure ReadFlow(Reader: TReader);
procedure WriteFlow(Writer: TWriter);
public
Name: String;
list: TStrings;
constructor create(component: TComponent)
override;
destructor destroy
override;
protected
procedure DefineProperties(Filer: TFiler)
override;
end;
PTempList = ^TTempList;
TTempList = class(TComponent)
private
procedure ReadTemp(Reader: TReader);
procedure WriteTemp(Writer: TWriter);
public
Name: String;
constructor create(component: TComponent)
override;
destructor destroy
override;
protected
procedure DefineProperties(Filer: TFiler)
override;
end;
var
Form1: TForm1;
Flow: TFlow;
implementation
{$R *.DFM}
constructor TTempList.create(component: TComponent);
begin
inherited Create(component);
name := '';
end;
destructor TTempList.destroy;
begin
inherited destroy;
end;
procedure TTempList.DefineProperties(Filer: TFiler);
begin
inherited DefineProperties(Filer);
Filer.DefineProperty('TempInfo',ReadTemp, WriteTemp, true);
end;
procedure TTempList.ReadTemp(Reader: TReader);
begin
Name := Reader.ReadString;
end;
procedure TTempList.WriteTemp(Writer: TWriter);
begin
Writer.WriteString(Name);
end;
constructor TFlow.create(component: TComponent);
begin
inherited create(component);
List := TStrings.Create;
end;
destructor TFlow.destroy;
begin
List.Free;
inherited destroy;
end;
procedure TFlow.DefineProperties(Filer: TFiler);
begin
inherited DefineProperties(Filer);
Filer.DefineProperty('FlowInfo',ReadFlow, WriteFlow, true);
end;
procedure TFlow.ReadFlow(Reader: TReader);
begin
Name := Reader.ReadString;
Reader.Read(list,sizeOf(List));
end;
procedure TFlow.WriteFlow(Writer: TWriter);
begin
Writer.WriteString(name);
Writer.Write(List,SizeOf(List));
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Flow := TFlow.Create(self);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
FileStream: TFileStream;
temp: TTempList;
begin
try
temp := TTempList.Create(self);
temp.Name := Edit2.Text;
Flow.Name := Edit1.Text;
------------------------------------------------------------
Flow.list.AddObject('tmplist',TObject(temp));
--------- ??????运行时出错,'abstract error'------------------
FileStream := TFileStream.Create('c:/flow.dat',fmCreate);
FileStream.WriteComponent(Flow);
finally
FileStream.Free;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
FileStream: TFileStream;
temp: PTempList;
begin
try
new(temp);
Temp^ := TTempList.Create(self);
Flow.destroy;
Flow := TFlow.Create(self);
Flow.Name := '';
FileStream := TFileStream.Create('c:/flow.dat',fmOpenRead);
FileStream.ReadComponent(Flow);
temp^ := TTempList(Flow.list.objects[0]);
Edit2.Text := Temp^.Name;
Edit1.Text := Flow.Name;
finally
FileStream.Free;
dispose(temp);
end;
end;
end.