Why am I receiving an error about Delphi incompatible types (array and dynamic array)?

纵然是瞬间 提交于 2019-11-28 13:52:26

You cannot assign an open array to a dynamic array. See Open Array Parameters.

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.


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>);

and your resultArray as TArray<TGenericHoldingResult>.

This will avoid having to declare a specific type for the array.

As noted by David, open arrays have a benefit since they have a broader use case, and should be used when possible.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!