HashTable 中是否可包含对象(100分)

  • 主题发起人 savenight
  • 开始时间
S

savenight

Unregistered / Unconfirmed
GUEST, unregistred user!
我在HashTable中存入一个TDatasource对象,可是读不出来。
调试时 显示它的值是 $F55100
 
当然可以啦,什么对象都可以存的。
 
...
type
interfaceA = record
ID: integer;
DataSource: tDataSource;
FieldName: string[128];
FieldCaption: array[0..Maxlanguage - 1] of string[128];
FieldType: TdxDBTreeListColumnClass;
Enabled: Boolean;
Visible: Boolean;
end;
...

//-----------------------------------------------------
function Tfrm_main.WriteInterfaceA(Key: ICollectible;
Value: IUnknown): Boolean;
var
KeyInteger: IInteger;
KeyValue: IInterfaceA;
pValue: interfaceA;
begin
KeyInteger := IInteger(Key);
KeyValue := IInterfaceA(Value);
pValue := KeyValue.GetValue;
write(FileFields, pValue);
result := true;
end;
//-------------------------------------------------------
procedure WriteInterfaceA;
var
pFunc: TAssociationProc;
begin
assignFile(FileFields, FieldFileName);
pFunc := frm_main.WriteInterfaceA;
try
rewrite(FileFields);
InterfaceAHashTable.ForEach(pFunc);
finally
closefile(FileFields);
end;
end;

//------------------------------------------
procedure Tfrm_options.Button1Click(Sender: TObject);
var
HashItem: IAssociation;
HashKey: ICollectible;
HashValue: ICollectible;
p:interfaceA;
begin
p.ID := strtoint(edt_id.text);
p.DataSource :=tdatasource( cb_ds.Items.Objects[cb_ds.ItemIndex]);
p.DataSource.DataSet.Close;
p.DataSource.DataSet.Open;
p.FieldName :=cb_fn.Items[cb_fn.itemindex];
p.FieldCaption[0]:=edt_fc0.text;
p.FieldCaption[1]:=edt_fc1.text;
p.FieldType :=tdxdbtreelistcolumnclass(cb_ft.Items.Objects[cb_ft.ItemIndex]);
p.Enabled :=cb_enable.checked;
p.Visible := cb_visible.checked;
HashKey := TInteger.Create(p.id);
HashValue := TinterfaceA.Create(p);
HashItem := TAssociation.Create(hashkey, hashvalue);
InterfaceAHashTable.Add(hashitem);
end;
//将数据写入hashtable.
//-----------------------------------------
procedure ini_interfaceA;
var
FieldFile: file of interfaceA;
p: interfaceA;
i: integer;
begin
assignfile(FieldFile, FieldFileName);
reset(FieldFile);
seek(FieldFile, 0);
setlength(ArrayInterfaceA,filesize(fieldfile));
with frm_maindo
begin
for i := 0 to filesize(fieldfile) - 1do
begin
read(FieldFile, p);
ArrayInterfaceA.ID := p.ID;
** ArrayInterfaceA.DataSource := tdatasource(p.DataSource);
ArrayInterfaceA.FieldName := p.FieldName;
ArrayInterfaceA.FieldCaption[0] := p.FieldCaption[0];
ArrayInterfaceA.FieldCaption[1] := p.FieldCaption[0];
ArrayInterfaceA.FieldType := p.FieldType;
ArrayInterfaceA.Enabled := p.Enabled;
ArrayInterfaceA.Visible := p.visible;
end;
end;
closefile(FieldFile);
end;
//读出数据。。。
//-------------------------------------------------------------
程序大概如上,当我读到 **的地方出错,发现datasource.name=''??
我还以为hashtable不可存入对象呢,
听吴兄再说两句。。。。
 
[red]ok,problem was solved,thanks Wu sir[/red]
 
接受答案了.
 
顶部