我正在函数中创建一个Tdataset
对象。
请找到以下代码:
procedure CreateDataset;
var
LDataset: TDataset;
begin
LDataset := TDataset.create(nil);
if LDataset = nil then
Exit;
with LDataset do
begin
FieldDefs.Clear;
FieldDefs.Add('NAME', ftString, 50);
FieldDefs.Add('Designation', ftString, 20);
FieldDefs.Add('Address', ftString,100);
FieldDefs.Add('ContactNum', ftString,12);
Open; // giving exception as 'AbstractError' at this line
end;
end;
打开数据集时,我收到异常抽象错误。 如果我有任何错误,请纠正我。
答案 0 :(得分:0)
好的,让我们看看:
procedure CreateDataset;
var
LDataset: TDataset;
begin
LDataset := TDataset.create(nil);
// ERROR. TDataset is an abstract class, don't instantiate it.
// Use a descendant class instead.
if LDataset = nil then
Exit;
// NONSENSE. It would never work. Maybe you're afraid about
// 'dataset not created'. If so, the constructor will raise an
// exception.
with LDataset do
begin
FieldDefs.Clear;
// FOR WHAT? You create this dataset a millisecond ago.
// It's empty.
FieldDefs.Add('NAME', ftString, 50);
// BTW, you should not create and fill components without
// reason. Try to use design-time abilities instead.
FieldDefs.Add('Designation', ftString, 20);
FieldDefs.Add('Address', ftString,100);
FieldDefs.Add('ContactNum', ftString,12);
Open;
// ERROR. Even if correct dataset class used, it will not
// open if it is not linked to some correct connection object.
end;
// LEAK. And now? You created the dataset. You opened it. You
// stored it nowhere. You do not return it. At the moment
// you'll leave the procedure, the opened dataset would be
// lost. It would never close and never be disposed until
// the end of program.
end;