动态数组与Variant应该如何转换?

解决方案 »

  1.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Grids;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        Procedure D(a:Variant);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}{ TForm1 }procedure TForm1.D(a: Variant);
    var
      iParm:Array of Integer;
      i,j:Integer;
    begin
      iParm:=a;
      j:=0;
      for i:=0 to High(iParm) do
        j:=j+iParm[i];  ShowMessage(IntToStr(j));
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      iParm:Array of Integer;
      a:Variant;
    begin
      SetLength(iParm,3);
      iParm[0]:=1;
      iParm[1]:=3;
      iParm[2]:=5;  a:=iParm;
      D(a);
    end;end.
      

  2.   

    All integer, real, string, character, and Boolean types are assignment-compatible with Variant. Expressions can be explicitly cast as variants, and the VarAsType and VarCast standard routines can be used to change the internal representation of a variant. The following code demonstrates the use of variants and some of the automatic conversions performed when variants are mixed with other types.var
      V1, V2, V3, V4, V5: Variant;
      I: Integer;
      D: Double;
      S: string;
    begin
      V1 := 1; { integer value }
      V2 := 1234.5678; { real value }
      V3 := 'Hello world!'; { string value }
      V4 := '1000'; { string value }
      V5 := V1 + V2 + V4; { real value 2235.5678}
      I := V1; { I = 1 (integer value) }
      D := V2; { D = 1234.5678 (real value) }
      S := V3; { S = 'Hello world!' (string value) }
      I := V4; { I = 1000 (integer value) }
      S := V5; { S = '2235.5678' (string value) }end;