dellphi可变数组

dellphi可变数组

unit Unit1;

interface

uses
  DB, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private    { Private declarations }
  public    { Public declarations }
  end;

var
  Form1: TForm1;


implementation

{$R *.dfm}

type
  TRec = array of TVarRec;

procedure addRec(var P: TRec; values: array of const);
var
  i: integer;
begin
  Setlength(P, High(values) + 1);
  for i := Low(P) to High(P) do
  begin
    P[i].VType := values[i].VType;
    case values[i].VType of
      vtInteger:
        P[i].VInteger := values[i].VInteger;
      vtInt64:
        P[i].VInt64 := values[i].VInt64;
      vtBoolean:
        P[i].VBoolean := values[i].VBoolean;
      VtChar:
        P[i].VPchar := values[i].VPchar;
      vtString:
        P[i].VString := values[i].VString;
      vtAnsiString:
        P[i].VAnsiString := values[i].VAnsiString;
      vtWideString:
        P[i].VWideString := values[i].VWideString;
      vtPointer:
        P[i].VPointer := values[i].VPointer;
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  p, p2: TRec; //array of TVarRec;
  i, l: integer;
  b:array of Byte;
begin
  addRec(p, ['中华人民共和国', 123]);
  l:=SizeOf(p);
  SetLength(b,l);
  Move(p, b[0], l);

  SetLength(p2, l);
  Move(b[0], p2, l);
  for i := low(p2) to high(p2) do
    case p2[i].VType of
      vtInteger: ShowMessage(IntToStr(p2[i].VInteger));         //中华人民共和国
      VtChar, vtString, vtAnsiString: ShowMessage(p[i].VPchar); //123
    end;
end;

end.

 

posted @ 2022-03-25 10:40  delphi中间件  阅读(93)  评论(0编辑  收藏  举报