制作类的动态数组的独立副本 [英] Making independent copies of Dynamic Arrays of classes

查看:76
本文介绍了制作类的动态数组的独立副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建涉及类(不是Records)的DynamicArrays的独立副本。我正在使用Delphi XE5。在下面的示例代码中,完成所有分配后,动态数组的所有副本都包含相同的值。已在>复制不会创建动态数组的独立副本,因为涉及到类,因此这是预期的行为。我遇到的问题是我无法避免使用类。类型定义是由Delphi通过读取WSDL生成的。这是一个巨大的文件,其中包含近5000行。试图修改长度的类型定义是不明智的,它将打开另一罐蠕虫。

I want to create independent copies of DynamicArrays involving class (not Records). I am using Delphi XE5. In the example code below, after all the assignments are done, all copies of the dynamic array contain the same values. It has been explained in "Copy" does not create independent copy of Dynamic Array that it is an expected behviour since classes are involved. The problem I run into is that I can't avoid using classes. The type definitions are generated by Delphi by reading in a WSDL. It is a monsterous file with close to 5000 lines in it. Trying to modifying type definitions at length woud not be advisable and will open another can of worms.

 TYPE
  TX = class(TRemotable)
  private
    <..vars and functions..>
  published
    <..properties..>
  end;

  TArray_Of_TX = array of TX;

  TDATA_RECORD = class(TRemotable)
  private
      <..vars and functions..>
  public
      destructor Destroy; override;
  published
      <..other properties..>
      property Y: TArray_Of_TX  Index (IS_OPTN or IS_UNBD) read FY write SetY stored ABooleanVariable;
  end;

 VAR
   X: TArray_Of_TX;
   DataRecord_1, DataRecord_2, DataRecord_3: TData_Record;

 BEGIN
   SetLength(X, 1);
   X[0] := T_X_Type.Create();

   X[0].var := 'some value 1';
   DataRecord_1 := TData_Record.create();
   DataRecord_1.Y := copy(X);

   X[0].var := 'some value 2';
   DataRecord_2 := TData_Record.create();
   DataRecord_2.Y := copy(X);

   X[0].var := 'some value 3';
   DataRecord_3 := TData_Record.create();
   DataRecord_3.Y := copy(X);

   X[0].var := 'some value 4';
   //At this time, I would like to have all copies of TArray_of_TX have different 
   //values for various member fields but they all have the same value--the last
   //value assigned to X[0].var!

认为 Copy()不是要为类对象工作,我尝试了 setLength(DataRecord_1.Y,1),但是它引发了编译器错误常量对象不能作为var传递一个简单的解决方案似乎是根据需要创建尽可能多的 X变量实例,但问题是它的数量可能很大(在设计时未知)。顺便说一句,那将是非常低效和无能为力的。还会引入跟踪何时使用哪个变量等的自身挑战。

Thinking that the Copy() is not going to work for class objects, I tried setLength(DataRecord_1.Y, 1) but it raised a compiler error "Constant object cannot be passed as var parameter". An easy solution may seem to be to create as many instances of "X" variable as needed but the problem is it could be quite a large number (not known at design time). Besided, that would be very inefficient and inelagent. It would also introduce its own challenges of keeping track of using which variable when, etc etc.

推荐答案

所以:

function CloneArray(original: TArray_Of_TX): TArray_Of_TX;
var
  i: integer;
  copy: TX;
begin
  Result.SetLength(SizeOf(original));
  for i:= 0 to SizeOf(original) -1 do begin
    copy:= TX.Create;
    copy.assign(original[i]);
    Result[i]:= copy;
  end; {for i}
end;

此版本不使用泛型,因为我不确定如何使用动态数组。

This version does not use generics, because I'm not sure how to create a generic version using a dynamic array.

这篇关于制作类的动态数组的独立副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆