数组 – 为什么我收到有关Delphi不兼容类型(数组和动态数组)的错误?

数组 – 为什么我收到有关Delphi不兼容类型(数组和动态数组)的错误?,第1张

概述(编辑:这是继 Are objects reference counted in Windows-targeted Delphi applications, and if so, what is its purpose?和 Dynamic arrays and memory management in Delphi之后). 我有两个类(TGenericHoldingSummary,TGeneric (编辑:这是继 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和lazy-loaded.
> 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;// lifecycleconstructor 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'
解决方法 您无法将打开的数组分配给动态数组.见 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;

可以在此处找到开放阵列的用例以及与动态阵列的不同之处的一个很好的总结:Open array parameters.

如果您有支持泛型的Delphi版本,则可以声明构造函数头:

constructor TGenericHoldingResultSet.Create(parent : TGenericHoldingSummary;   const resArr : TArray<TGenericHoldingResult>);

和您的resultArray为TArray< TGenericHoldingResult>.

这将避免必须为数组声明特定类型.

正如DavID所指出的,开放阵列具有一个优势,因为它们具有更广泛的用例,并且应该尽可能使用.

总结

以上是内存溢出为你收集整理的数组 – 为什么我收到有关Delphi不兼容类型(数组和动态数组)的错误?全部内容,希望文章能够帮你解决数组 – 为什么我收到有关Delphi不兼容类型(数组和动态数组)的错误?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1246040.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存