{把不同数据类型返回字符串的函数} function Fun1(arr: array of const): string; var i: Integer; begin Result := ''; for i := Low(arr) to High(arr) do begin case arr[i].VType of vtInteger : Result := Result + IntToStr(arr[i].VInteger) + ' '; vtBoolean : Result := Result + BoolToStr(arr[i].VBoolean, True) + ' '; vtChar : Result := Result + arr[i].VChar + ' '; vtExtended : Result := Result + FloatToStr(arr[i].VExtended^) + ' '; vtString : Result := Result + PShortString(arr[i].VString)^ + ' '; vtPointer : Result := Result + IntToStr(Integer(arr[i].VPointer)) + ' '; vtPChar : Result := Result + arr[i].VPChar + ' '; vtObject : Result := Result + arr[i].VObject.ClassName + ' '; vtClass : Result := Result + arr[i].VClass.ClassName + ' '; vtWideChar : Result := Result + arr[i].VWideChar + ' '; vtPWideChar : Result := Result + arr[i].VPWideChar + ' '; vtAnsiString: Result := Result + PAnsiChar(arr[i].VAnsiString)^ + ' '; vtCurrency : Result := Result + CurrToStr(arr[i].VCurrency^) + ' '; vtVariant : Result := Result + string(arr[i].VVariant^) + ' '; vtInterface : Result := Result + IntToStr(Integer(arr[i].VInterface)) + ' '; vtWideString: Result := Result + PWideChar(arr[i].VWideString) + ' '; vtInt64 : Result := Result + IntToStr(arr[i].VInt64^) + ' '; end; end; end;
{简化上一个函数} function Fun2(const arr: array of const): string; var i: Integer; const n = #32; begin Result := ''; for i := Low(arr) to High(arr) do with arr[i] do begin case VType of 0 : Result := Result + IntToStr(VInteger) + n; 1 : Result := Result + BoolToStr(VBoolean, True) + n; 2 : Result := Result + VChar + n; 3 : Result := Result + FloatToStr(VExtended^) + n; 4 : Result := Result + PShortString(VString)^ + n; 5 : Result := Result + IntToStr(Integer(VPointer)) + n; 6 : Result := Result + VPChar + n; 7 : Result := Result + VObject.ClassName + n; 8 : Result := Result + VClass.ClassName + n; 9 : Result := Result + VWideChar + n; 10: Result := Result + VPWideChar + n; 11: Result := Result + PAnsiChar(VAnsiString)^ + n; 12: Result := Result + CurrToStr(VCurrency^) + n; 13: Result := Result + string(VVariant^) + n; 14: Result := Result + IntToStr(Integer(VInterface)) + n; 15: Result := Result + PWideChar(VWideString) + n; 16: Result := Result + IntToStr(VInt64^) + n; end; end; end;
{获取类型名的函数} function Fun3(const arr: array of const): string; var i: Integer; const n = sLineBreak; begin Result := ''; for i := Low(arr) to High(arr) do with arr[i] do begin case VType of 0 : Result := Result + 'Integer' + n; 1 : Result := Result + 'Boolean' + n; 2 : Result := Result + 'Char' + n; 3 : Result := Result + 'Extended' + n; 4 : Result := Result + 'String' + n; 5 : Result := Result + 'Pointer' + n; 6 : Result := Result + 'PChar' + n; 7 : Result := Result + 'Object' + n; 8 : Result := Result + 'Class' + n; 9 : Result := Result + 'WideChar' + n; 10: Result := Result + 'PWideChar' + n; 11: Result := Result + 'AnsiString'+ n; 12: Result := Result + 'Currency' + n; 13: Result := Result + 'Variant' + n; 14: Result := Result + 'Interface' + n; 15: Result := Result + 'WideString'+ n; 16: Result := Result + 'Int64' + n; end; end; end;
{测试} procedure TForm1.Button1Click(Sender: TObject); var a: Integer; b: Boolean; c: Char; d: Extended; e: ShortString; f: Pointer; g: PChar; h: TButton; i: TClass; j: WideChar; k: PWideChar; l: AnsiString; m: Currency; n: Variant; o: IInterface; p: WideString; q: Int64; begin a := 1; b := True; c := 'a'; d := 2; e := 'S'; f := Pointer(3); g := 'P'; h := TButton(Sender); i := TForm; j := #19975; k := '一'; l := 'A'; m := 4; n := 5; //o; p := '万一'; q := 7;
ShowMessage(Fun1([a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q])); ShowMessage(Fun2([a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q])); {结果如下:} {1 True a 2 S 3 P TButton TForm 万 一 A 4 5 0 万一 7 }