为什么我会收到有关 Delphi 不兼容类型(数组和动态数组)的错误消息? [英] Why am I receiving an error about Delphi incompatible types (array and dynamic array)?
问题描述
(编辑:这是继在面向 Windows 的 Delphi 应用程序中计数的对象引用,以及如果是,它的目的是什么? 和 Delphi 中的动态数组和内存管理).
我有两个类(TGenericHoldingSummary
、TGenericHoldingResultSet
)和一个记录(TGenericHoldingResult
).
I have two classes (TGenericHoldingSummary
, TGenericHoldingResultSet
) and one record (TGenericHoldingResult
).
TGenericHoldingSummary
包含一个TGenericHoldingResultSet
,它被设置为nil
并在需要时从数据库中延迟加载.TGenericHoldingResultSet
包含一个动态的TGenericHoldingResult
记录数组.
TGenericHoldingSummary
contains a singleTGenericHoldingResultSet
, which is set tonil
and lazy-loaded from the database if and when required.- The
TGenericHoldingResultSet
contains a dynamic array ofTGenericHoldingResult
records.
在下面,错误是在 TGenericHoldingResultSet
构造函数中的赋值.
In the below, the error is at the assignment in the TGenericHoldingResultSet
constructor.
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'
推荐答案
不能将开放数组分配给动态数组.请参阅开放阵列参数
.
You cannot assign an open array to a dynamic array. See Open Array Parameters
.
注意:开放数组参数的语法类似于动态数组类型的语法,但它们的含义并不相同.前面的示例创建了一个函数,该函数采用任何 Char 元素数组,包括(但不限于)动态数组.要声明必须是动态数组的参数,您需要指定一个类型标识符:
Note: The syntax of open array parameters resembles that of dynamic array types, but they do not mean the same thing. The previous example creates a function that takes any array of Char elements, including (but not limited to) dynamic arrays. To declare parameters that must be dynamic arrays, you need to specify a type identifier:
type TDynamicCharArray = array of Char;
function Find(const A: TDynamicCharArray): Integer;
开放数组
用例的一个很好的总结以及与动态数组的区别可以在这里找到:打开数组参数
.
A good summary of the use case of open arrays
and the difference with a dynamic array can be found here: Open array parameters
.
如果你有一个支持泛型的 Delphi 版本,可以声明构造函数头:
If you have a Delphi version that supports generics, it is possible to declare the constructor header:
constructor TGenericHoldingResultSet.Create(parent : TGenericHoldingSummary;
const resArr : TArray<TGenericHoldingResult>);
和你的 resultArray
作为 TArray
.
这将避免必须为数组声明特定类型.
This will avoid having to declare a specific type for the array.
正如 David 所指出的,开放数组有一个好处,因为它们有更广泛的用例,应该尽可能使用.
As noted by David, open arrays have a benefit since they have a broader use case, and should be used when possible.
这篇关于为什么我会收到有关 Delphi 不兼容类型(数组和动态数组)的错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!