unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, ExtCtrls;type
  TValue = Record
    str1: string;
    Int: integer;
    str2: string;
    str3: string;
  End;
  PValue = ^TValue;
  AValue = Array Of PValue;  TForm1 = class(TForm)
    TreeView: TTreeView;
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    procedure AddArray(Var List: TList);
    function GetValue: AValue;
  public
    { Public declarations }
  end;var
  Form1: TForm1;
  List: TList;implementation{$R *.dfm}procedure TForm1.AddArray(var List: TList);
Var
  i: integer;
  ArrayValue: Array Of PValue;
begin
  SetLength(ArrayValue,4);
  For i := 0 To 3 Do
  Begin
      New(ArrayValue[i]);
      ArrayValue[i]^.str1 := 'aaa' + IntToStr(i);
      ArrayValue[i]^.str2 := 'bbb' + IntToStr(i);
      ArrayValue[i]^.str3 := 'ccc' + IntToStr(i);
  End;
  IF Assigned(List) Then List.Destroy;
  IF Not Assigned(ArrayValue) Then Raise Exception.Create('Null of ArrayValue');
  List := TList.Create;
  List.Add(ArrayValue);
  ShowMessage('The ArrayValue has Add into List Item');
end;procedure TForm1.Button1Click(Sender: TObject);
begin
  AddArray(List);
end;procedure TForm1.Button3Click(Sender: TObject);
begin
//
end;procedure TForm1.Button2Click(Sender: TObject);
Var
  a: AValue;
  i: integer;
begin
  a := GetValue;
  For i := Low(a) To High(a) Do
  Begin
    ShowMessage(a[i]^.str1);//这里取数据不出来,为什么?
  End;
end;function TForm1.GetValue: AValue;
begin
  Result := Nil;
  IF Not Assigned(List) Then Exit;
  Result := List.Items[0];
end;end.我把ArrayValue数组加到List中,然后通过GetValue取出来,但是取出来的没数据,为什么呢?谢谢!

解决方案 »

  1.   

    button1click中的ArrayValue: Array Of PValue;
    定义成全局变量....
      

  2.   

    楼主好象是把一个数组作为一个Item放到List中的。
    List中只能放指针,而你这里放的是一个动态数组,所以取不出来东西,给你修改一下:type
      TValue = Record
        str1: string;
        Int: integer;
        str2: string;
        str3: string;
      End;
      PValue = ^TValue;
      AValue = Array Of PValue;
      PAValue=^AValue;///////////////定义一个指向AValue的指针
    ..............
      private
        procedure AddArray(Var List: TList);
        function GetValue: PAValue;////////函数返回修改为一个PAValue指针
    ...........procedure TForm1.AddArray(var List: TList);
    Var
      i: integer;
      PV:PAValue;
    begin
      new(PV);
      SetLength(PV^,4);
      For i := 0 To 3 Do
      Begin
          New(PV^[i]);
          PV^[i]^.str1 := 'aaa' + IntToStr(i);
          PV^[i]^.str2 := 'bbb' + IntToStr(i);
          PV^[i]^.str3 := 'ccc' + IntToStr(i);
      End;
      List := TList.Create;
      List.Add(PV);
    end;..............
    procedure TForm1.Button2Click(Sender: TObject);
    Var
      PV:PAValue;
      i: integer;
    begin
      PV := GetValue;
      ShowMessage(PV^[0]^.str1);//能够得到PV^[i]^.str1值
    ..............function TForm1.GetValue: PAValue;/////返回为PAValue
    begin
      Result := Nil;
    ..............
      Result := List.Items[0];
    end;
      

  3.   

    List.Add(ArrayValue);
    ///这句话只执行完毕之后list仍然是空的...
    for I:=0 to 3 do
    list.Add(arrayvalue[I]);
      

  4.   

    gzmhero(hihihi) 老大
    这样为什么不行?
    for I:=0 to 3 do
    list.Add(arrayvalue[I]);
      

  5.   

    代碼很亂。
    比如:  IF Assigned(List) Then List.Destroy; 很少直接這樣寫的 雖然也沒錯我以前測試過 
      你把TList對象作為函數參數傳遞 然後在函數中.Add,到一定程度時會出錯的
      

  6.   

    heluqing(鉴之小河〖挣大钱娶美女〗) ( ) 信誉:100 这样也可以的,但是楼主的要求可能不是把每个元素作为一个Item放到列表里。
    应该是将一个数组做为一个元素放到List中。
      

  7.   

    gzmhero(hihihi) ( ) 信誉:105 
    那么说这样也行了
      List.Add(@ArrayValue);
    但是我这么做了之后,list还是()没有任何值添加进去了,可能真的如
    beyondtkl(大龙驹<干掉小日本>) ( ) 信誉:116 所说
     你把TList對象作為函數參數傳遞 然後在函數中.Add,到一定程度時會出錯的
    真的是这样吗?
    我的没有添加上...
      

  8.   

    heluqing(鉴之小河〖挣大钱娶美女〗) ( ) 信誉:100  List.Add(@ArrayValue);
    这样也可以的。但 ArrayValue: Array Of PValue;要是全局变量才行。
    然后取出的时候要function TForm1.GetValue: APValue;才行。因为List中的是指针。