Question : Delphi variant array

I have got a variant array of string in Delphi. How can I check what values have been put into the array i.e. showmesage or some visual check ?

Answer : Delphi variant array

I use these utility functions :

Var MyVar:Variant;

begin
 MyVar:=... // do some initialization

 ShowMessageFmt( 'MyVar is of type %s and it's value is '#13#10'%s',
    [VariantTypeName(MyVar),VariantAsString(MyVar)]);
end;
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
function VariantBasicTypeName(varVar: Variant):String;
var
  basicType  : Integer;
begin
  basicType := VarType(varVar) and VarTypeMask;
  case basicType 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';
    varByte      : Result := 'Byte';
{$ifdef VER140}
    varWord      : Result := 'Word';
    varLongWord  : Result := 'LongWord';
    varInt64     : Result := 'Int64';
{$endif}    
    varStrArg    : Result := 'StrArg';
    varString    : Result := 'String';
    varAny       : Result := 'Any';
    varTypeMask  : Result := 'TypeMask';
    Else Result := Format('Custom(%d)',[basicType]);
  end;
end;

function VariantTypeName(varVar: Variant):String;
var
  ExtType,i  : Integer;
begin
 ExtType := VarType(varVar);
 Result:=VariantBasicTypeName(varVar);
 if (ExtType And varArray)>0 Then
  begin
   Result:=Result+'[';
   for i:=1 to VarArrayDimCount(varVar) do Result:=Result+Format('%d,',[VarArrayHighBound(varVar,i)-VarArrayLowBound(varVar,i)+1]);
   Result[Length(Result)]:=']';
  end;
 if (ExtType And varByRef)>0 Then Result:='@'+Result;
end;

function VariantAsString(varVar:Variant):String;
Var
// L:TStringList;
 basicType,i,j,Dim : Integer;
begin
 basicType := VarType(varVar) and VarTypeMask;
 if VarIsArray(varVar) then
  begin
   Dim:=VarArrayDimCount(varVar);
   if Dim>2 Then
    begin
     Result:='Array '+VariantTypeName(varVar);
     Exit;
    end;
   Result:='[';
   if Dim=1
    Then for i:=VarArrayLowBound(varVar,1) to VarArrayHighBound(varVar,1) do Result:=Result+VariantAsString(varVar[i])+','
    Else for i:=VarArrayLowBound(varVar,1) to VarArrayHighBound(varVar,1) do
     begin
      Result:=Result+#13#10'  [';
      for j:=VarArrayLowBound(varVar,2) to VarArrayHighBound(varVar,2) do Result:=Result+VariantAsString(varVar[i,j])+',';
      Result[Length(Result)]:=']';
      Result:=Result+' ,';
     end;
   Result[Length(Result)]:=']';
  end else
  begin
   if VarIsEmpty(varVar) Then Result:='(Empty)' Else
   if VarIsNull(varVar) Then Result:='(Null)' Else Result:=VarToStr(varVar);
  end;
end;
Random Solutions  
 
programming4us programming4us