之前应该参考一下: 关于开放数组参数
作为参数的开放数组, 有时数组的成员类型是不确定的, 此时应该使用 array of const 定义; 详细举例:
从这个例子中还能得到的启示是(根据网友的提示, 下面可能理解错了!):
//这是在 System 单元定义的一组标识数据类型的常量:
vtInteger = ;
vtBoolean = ;
vtChar = ;
vtExtended = ;
vtString = ;
vtPointer = ;
vtPChar = ;
vtObject = ;
vtClass = ;
vtWideChar = ;
vtPWideChar = ;
vtAnsiString = ;
vtCurrency = ;
vtVariant = ;
vtInterface = ;
vtWideString = ;
vtInt64 = ; //这是定义在 System 单元关于数据类型的一个结构:
TVarRec = record
case Byte of
vtInteger: (VInteger: Integer; VType: Byte);
vtBoolean: (VBoolean: Boolean);
vtChar: (VChar: Char);
vtExtended: (VExtended: PExtended);
vtString: (VString: PShortString);
vtPointer: (VPointer: Pointer);
vtPChar: (VPChar: PChar);
vtObject: (VObject: TObject);
vtClass: (VClass: TClass);
vtWideChar: (VWideChar: WideChar);
vtPWideChar: (VPWideChar: PWideChar);
vtAnsiString: (VAnsiString: Pointer);
vtCurrency: (VCurrency: PCurrency);
vtVariant: (VVariant: PVariant);
vtInterface: (VInterface: Pointer);
vtWideString: (VWideString: Pointer);
vtInt64: (VInt64: PInt64);
end;
作为参数的开放数组, 有时数组的成员类型是不确定的, 此时应该使用 array of const 定义; 详细举例:
unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls; type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
end; var
Form1: TForm1; implementation {$R *.dfm} {把不同数据类型返回字符串的函数}
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
: Result := Result + IntToStr(VInteger) + n;
: Result := Result + BoolToStr(VBoolean, True) + n;
: Result := Result + VChar + n;
: Result := Result + FloatToStr(VExtended^) + n;
: Result := Result + PShortString(VString)^ + n;
: Result := Result + IntToStr(Integer(VPointer)) + n;
: Result := Result + VPChar + n;
: Result := Result + VObject.ClassName + n;
: Result := Result + VClass.ClassName + n;
: Result := Result + VWideChar + n;
: Result := Result + VPWideChar + n;
: Result := Result + PAnsiChar(VAnsiString)^ + n;
: Result := Result + CurrToStr(VCurrency^) + n;
: Result := Result + string(VVariant^) + n;
: Result := Result + IntToStr(Integer(VInterface)) + n;
: Result := Result + PWideChar(VWideString) + n;
: 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
: Result := Result + 'Integer' + n;
: Result := Result + 'Boolean' + n;
: Result := Result + 'Char' + n;
: Result := Result + 'Extended' + n;
: Result := Result + 'String' + n;
: Result := Result + 'Pointer' + n;
: Result := Result + 'PChar' + n;
: Result := Result + 'Object' + n;
: Result := Result + 'Class' + n;
: Result := Result + 'WideChar' + n;
: Result := Result + 'PWideChar' + n;
: Result := Result + 'AnsiString'+ n;
: Result := Result + 'Currency' + n;
: Result := Result + 'Variant' + n;
: Result := Result + 'Interface' + n;
: Result := Result + 'WideString'+ n;
: 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 := ;
b := True;
c := 'a';
d := ;
e := 'S';
f := Pointer();
g := 'P';
h := TButton(Sender);
i := TForm;
j := #19975;
k := '一';
l := 'A';
m := ;
n := ;
//o;
p := '万一';
q := ; 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 } ShowMessage(Fun3([a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q]));
{结果如下:
Integer
Boolean
Char
Extended
String
Pointer
PChar
Object
Class
WideChar
PWideChar
AnsiString
Currency
Variant
Interface
WideString
Int64
}
end; {我试试没有在 TVarRec 中的类型是怎么归类的}
procedure TForm1.Button2Click(Sender: TObject);
var
a: Byte;
b: Word;
c: Cardinal;
d: Double;
e: Real;
f: TStrings;
begin
ShowMessage(Fun3([a,b,c,d,e,f]));
{结果如下:
Integer
Integer
Integer
Extended
Extended
Object
}
end; end.
从这个例子中还能得到的启示是(根据网友的提示, 下面可能理解错了!):
//不超过 4 字节的简单类型, 在内存中只有一个存放处.
Integer;
Boolean;
Char;
WideChar; //超过 4 字节的类型(包括字符串), 在内存中有两个存放处: 一个是指针位置; 一个是数据位置.
Int64;
Extended;
Currency;
Variant;
ShortString;
AnsiString;
WideString; //指针: 只有 4 字节大小.
Pointer;
PChar(PAnsiChar);
PWideChar; //对象: 复杂了...
Object
Class;
IInterface;