【问题标题】:Why am I receiving an error about Delphi incompatible types (array and dynamic array)?为什么我收到有关 Delphi 不兼容类型(数组和动态数组)的错误?
【发布时间】: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的后续)。

我有两个班级(TGenericHoldingSummaryTGenericHoldingResultSet)和一个记录(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


    【解决方案1】:

    您不能将开放数组分配给动态数组。见Open Array Parameters

    注意:开放数组参数的语法类似于动态数组类型,但它们的含义不同。前面的示例创建了一个函数,该函数接受任何 Char 元素数组,包括(但不限于)动态数组。声明必须是动态数组的参数,需要指定类型标识符:

    type TDynamicCharArray = array of Char;
    function Find(const A: TDynamicCharArray): Integer;
    

    open arrays 的用例以及与动态数组的区别可以在这里找到:Open array parameters


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

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

    你的resultArrayTArray&lt;TGenericHoldingResult&gt;

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

    正如 David 所说,开放数组有一个好处,因为它们有更广泛的用例,应该尽可能使用。

    【讨论】:

    • 这很有帮助,但现在我收到另一个错误。我已更改添加类型TGenericHoldingResultDynamicArray = Array of TGenericHoldingResult; 并更改了参数,但现在在分配行[DCC Error] GenericHolding.pas(261): E2010 Incompatible types: 'TGenericHoldingResultDynamicArray' and 'Dynamic array' 上收到不同的错误。
    • resultArray 也必须声明为TGenericHoldingResultDynamicArray
    • 不,它是强类型的,这是为了程序员的利益。
    • 最好不要声明新类型,而是使用通用语法TArray&lt;T&gt;。泛型具有更灵活的类型兼容性。通过必要性。在处理数组时,您可以利用它来发挥自己的优势。
    • @user 我认为很多不了解的 Delphi 开发人员在开放数组会更好的情况下更好地使用动态数组作为参数。开放数组的优点是它们更灵活。他们可以接受的不仅仅是动态数组。现在,有些时候参数应该是 dyn 数组,但如果可以使用开放数组,则应该使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-07
    • 1970-01-01
    • 2014-04-23
    • 2015-09-16
    • 1970-01-01
    • 2017-05-06
    相关资源
    最近更新 更多