We help IT Professionals succeed at work.

Delphi variant array

woodsy37
woodsy37 asked
on
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 ?
Comment
Watch Question

Geert GOracle dba
Top Expert 2009

Commented:
are you sure they are of type string ?
a variant can be anything ...
Top Expert 2010

Commented:
something like this:

procedure TForm1.Button1Click(Sender: TObject);
var
  A: Variant;
begin
  A := VarArrayCreate([0, 1], varVariant);
  A[0] := 1;
  A[1] := 'Hello world';
  if (VarType(A[0]) = varString) or (VarType(A[0]) = varOleStr) then
    ShowMessage( 'A[0] is a string???' )
  else
    ShowMessage( 'A[0] isn''t a string!!!' );

  if (VarType(A[1]) = varString) or (VarType(A[1]) = varOleStr) then
    ShowMessage( 'A[1] is a string!!!' )
  else
    ShowMessage( 'A[1] isn''t a string??? ' + IntToStr(Integer( VarType(A[1]) )) );
end;
Freelance Project Manager
Top Expert 2010
Commented:
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;
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;

Open in new window

Top Expert 2010

Commented:
Hi woodsy37

Why didn't you accept my solution http:#32960088 ? I proposed you to use the same VarType function.
Geert GOracle dba
Top Expert 2009

Commented:
i'd leave the answer as is, it's the most complete
Top Expert 2010

Commented:
@Geert_Gruwez

I didn't ask to decline epasquier's solution, I asked to accept my solution (for example, as assisted solution), because my code was correct and I did everything the asker asked.
At least I asked the asker why my solution didn't suit him

Commented:
>> How can I check what values have been put into the array i.e. showmesage or some visual check ?
In this question, surely epasquier did the right solution and for more better check! Aflarin's got it right also in focusing the variant string, so he must have the assisted solution.   But the author is the winner.