【发布时间】:2015-07-14 00:14:35
【问题描述】:
我有一个允许用户选择记录的表单,然后此表单返回记录的 ID 和调用表单可能需要的任意数量的字段值。为此,我有一个函数来处理创建选择表单并将所有值传入和传出调用表单:
Function Execute(AOwner: TComponent; AConnection: TADOConnection;
AEditor: String; AButtons: TViewButtons; Var AID: Integer;
Var ARtnVals: Array of Variant): TModalResult;
Var
I: Integer;
Begin
frmSelectGrid := TfrmSelectGrid.Create(AOwner);
Try
With frmSelectGrid Do
Begin
Connection := AConnection;
Editor := AEditor;
Buttons := AButtons;
ID := AID;
Result := ShowModal;
If Result = mrOK Then
Begin
AID := ID;
//VarArrayRedim(ARtnVals, Length(RtnVals)); !! Won't compile
//SetLength(ARtnVals, Length(RtnVals)); !! Won't compile either
For I := 0 To High(RtnVals) Do
ARtnVals[I] := RtnVals[I]; // Causes runtime exception
End;
End;
Finally
FreeAndNil(frmSelectGrid);
End;
End;
选择器表单具有以下公共属性:
public
Connection: TADOConnection;
Editor: String;
Buttons: TViewButtons;
ID: Integer;
RtnVals: Array of Variant;
end;
在确定点击中,我有以下代码:
Var
I, Idx, C: Integer;
// Count how many fields are being returned
C := 0;
For I := 0 To Config.Fields.Count - 1 Do
If Config.Fields[I].Returned Then
Inc(C);
// If no fields to be returned, then just exit.
If C = 0 Then
Exit;
// Set the size of the RtnVals and assign the field values to the array.
SetLength(RtnVals, C);
Idx := 0;
For I := 0 To Config.Fields.Count - 1 Do
If Config.Fields[I].Returned Then
Begin
RtnVals[Idx] := aqItems.FieldByName(Config.Fields[I].FieldName).Value;
Inc(Idx);
End;
因此,一旦用户单击“确定”选择记录,RtnVals 数组就会填充要返回的字段的字段值。我现在需要将这些值复制到 Execute 函数中的 ARtnVals,以便它们返回到调用表单。
我的问题是如何设置 ARtnVals 数组的大小以便我可以复制字段? SetLength 不像上面的 OK 点击函数那样工作。 VarArrayRedim 也不起作用。
【问题讨论】: