【发布时间】:2014-04-04 02:59:42
【问题描述】:
(编辑:这是Are objects reference counted in Windows-targeted Delphi applications, and if so, what is its purpose?和Dynamic arrays and memory management in Delphi的后续)。
我有两个班级(TGenericHoldingSummary、TGenericHoldingResultSet)和一个记录(TGenericHoldingResult)。
-
TGenericHoldingSummary包含一个TGenericHoldingResultSet,它设置为nil,并在需要时从数据库中延迟加载。 -
TGenericHoldingResultSet包含一个动态数组TGenericHoldingResult记录。
在下面,错误在于TGenericHoldingResultSet 构造函数中的赋值。
TGenericHoldingResult = record
code : Integer;
level : String;
msg : String;
end;
TGenericHoldingResultSet = class(TObject)
public
// Lifecycle
constructor Create(parent : TGenericHoldingSummary; resArr : Array of TGenericHoldingResult);
destructor Destroy;
// Accessors
function ResultCount() : Integer;
function Result(i : Integer) : TGenericHoldingResult;
private
// Variables
summary : TGenericHoldingSummary;
resultArray : Array of TGenericHoldingResult;
end;
TGenericHoldingSummary = class(TObject)
public
// Note that the summary object 'owns' the results, and deallocates
// its memory in the destructor.
function getResultSet: TGenericHoldingResultSet;
private
// Member variables
resultSet: TGenericHoldingResultSet;
end;
// Note that the summary object 'owns' the results, and deallocates
// its memory in the destructor.
function TGenericHoldingSummary.getResultSet() : TGenericHoldingResultSet;
var
sql : String;
i : Integer;
resultArray : Array of TGenericHoldingResult;
begin
if resultSet = nil then
begin
// Get results via SQL.
SetLength(resultArray, holding.clientDataSet.RecordCount);
for i := 0 to holding.clientDataSet.RecordCount - 1 do
begin
resultArray[i].code := holding.clientDataSet.FieldByName('code').AsInteger;
resultArray[i].level := holding.clientDataSet.FieldByName('level').AsString;
resultArray[i].msg := holding.clientDataSet.FieldByName('message').AsString;
end;
resultSet := TGenericHoldingResultSet.Create(self, resultArray);
end;
result := resultSet;
end;
// Lifecycle
constructor TGenericHoldingResultSet.Create(parent : TGenericHoldingSummary; resArr : Array of TGenericHoldingResult);
begin
summary := parent;
// The following *should* work, shouldn't it?
// E.g., seeing as dynamic arrays a reference counted in Delphi for
// all platforms, this should simply increment the reference count.
resultArray := resArr;
end;
错误如下:
[DCC Error] GenericHolding.pas(302): E2010 Incompatible types: 'Dynamic array' and 'Array'
【问题讨论】:
标签: arrays delphi dynamic-data