Variants 单元下的 VarType 函数可以获取变量类型, 但它返回的只是一个数字; 需要再结合一下在 System 单元里定义的这些常量:
varEmpty    = $0000;
varNull     = $0001;
varSmallint = $0002;
varInteger  = $0003;
varSingle   = $0004;
varDouble   = $0005;
varCurrency = $0006;
varDate     = $0007;
varOleStr   = $0008;
varDispatch = $0009;
varError    = $000A;
varBoolean  = $000B;
varVariant  = $000C;
varUnknown  = $000D;
varShortInt = $0010;
varByte     = $0011;
varWord     = $0012;
varLongWord = $0013;
varInt64    = $0014;
varStrArg   = $0048;
varString   = $0100;
varAny      = $0101;
varTypeMask = $0FFF;
varArray    = $2000;
varByRef    = $4000;

函数及测试如下:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{函数}
function GetVarType(const v: Variant): string;
begin
  case VarType(v) of
    varEmpty    : Result := 'Empty';
    varNull     : Result := 'Null';
    varSmallint : Result := 'Smallint';
    varInteger  : Result := 'Integer';
    varSingle   : Result := 'Single';
    varDouble   : Result := 'Double';
    varCurrency : Result := 'Currency';
    varDate     : Result := 'Date';
    varOleStr   : Result := 'OleStr';
    varDispatch : Result := 'Dispatch';
    varError    : Result := 'Error';
    varBoolean  : Result := 'Boolean';
    varVariant  : Result := 'Variant';
    varUnknown  : Result := 'Unknown';
    varShortInt : Result := 'ShortInt';
    varByte     : Result := 'Byte';
    varWord     : Result := 'Word';
    varLongWord : Result := 'LongWord';
    varInt64    : Result := 'Int64';
    varStrArg   : Result := 'StrArg';
    varString   : Result := 'String';
    varAny      : Result := 'Any';
    varTypeMask : Result := 'TypeMask';
    varArray    : Result := 'Array';
    varByRef    : Result := 'ByRef';
  end;
end;

{测试}
procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
  w: Word;
  s: string;
  d: Double;
begin
  ShowMessage(GetVarType(i)); {Integer}
  ShowMessage(GetVarType(w)); {Word}
  ShowMessage(GetVarType(s)); {string}
  ShowMessage(GetVarType(d)); {Double}
end;

end.

这里有个类似的东西: http://www.cnblogs.com/del/archive/2008/05/09/1158858.html#1190739

posted on 2008-05-09 23:01  万一  阅读(5629)  评论(4编辑  收藏  举报