function CStr(v:Variant):string;
var
vs: variant;
ret: string;
a: integer;
b: integer;
begin
try
if VarIsArray(v) then begin
a := VarArrayLowBound(v,1);
b := VarArrayHighBound(v, 1);
while a <= b do
begin
VarCast(vs,v[a],varString);
ret += VarToStr(vs);
if a <> b then ret += ', ';
inc(a);
end;
Result := '[' + ret + ']';
exit;
end;
VarCast(vs,v,varString);
Result := VarToStr(vs);
except on E: Exception do
Result := E.Message;
end;
end;
Example Use:
uses
Variants ...
var
v: variant;
begin
WriteLn('boolean test: ' + cstr(true));
WriteLn('int test: ' + cstr(32));
WriteLn('str test: ' + cstr('this is my string'));
v := VarArrayCreate([0, 9], varInteger);
for i := 0 to 9 do v[i]:= i;
WriteLn('test: ' + cstr(v));
VarClear(v);
v := VarArrayCreate([0, 3], varOleStr );
for i := 0 to 3 do v[i]:= StringOfChar( chr(i+$41), 5) ;
WriteLn('test 2: ' + cstr(v));
VarClear(v);