SmartPascal
VarType
Function
Gives the current type of a Variant variable Variants unit
 function VarType ( const VariantVariable : Variant ) : Integer;
Description
The VarType function returns an integer representing the current data type for a Variant variable VariantVariable.
 
The data type is a 16 bit value, split into a high order 4 bits and a low order 12 bits. The high order bits give a meta data type:
 
varArray  : If the Variant holds an array of data
varByRef  : If the Variant indirectly refers to data

 
The low order bits contain one of the following values:
 
varEmpty
varNull
varSmallint
varInteger
varSingle
varDouble
varCurrency
varDate
varOleStr
varDispatch
varError
varBoolean
varVariant
varUnknown
varShortInt
varByte
varWord
varLongWord
varInt64
varStrArg
varString
varAny

 
Use VarTypeMask to filter out the meta part when looking for the basic data type of a Variant.
Related commands
Variant A variable type that can hold changing data types
VarTypeMask Mask for the meta-type part of a Variant variable
 
Example code : Illustrating a few Variant types
var
  myVar : Variant;

begin
  // Assign various values to a Variant
  // and then show the resulting Variant type
  ShowMessage('Variant value = not yet set');
  ShowBasicVariantType(myVar);

  // Simple value
  myVar := 123;
  ShowMessage('Variant value = 123');
  ShowBasicVariantType(myVar);

  // Calculated value using a Variant and a constant
  myVar := myVar + 456;
  ShowMessage('Variant value = 123 + 456');
  ShowBasicVariantType(myVar);

  myVar := 'String '+IntToStr(myVar);
  ShowMessage('Variant value = String 579');
  ShowBasicVariantType(myVar);
end;

// Show the type of a variant
procedure TForm1.ShowBasicVariantType(varVar: Variant);
var
  typeString : string;
  basicType  : Integer;

begin
  // Get the Variant basic type :
  // this means excluding array or indirection modifiers
  basicType := VarType(varVar) and VarTypeMask;

  // Set a string to match the type
  case basicType of
    varEmpty     : typeString := 'varEmpty';
    varNull      : typeString := 'varNull';
    varSmallInt  : typeString := 'varSmallInt';
    varInteger   : typeString := 'varInteger';
    varSingle    : typeString := 'varSingle';
    varDouble    : typeString := 'varDouble';
    varCurrency  : typeString := 'varCurrency';
    varDate      : typeString := 'varDate';
    varOleStr    : typeString := 'varOleStr';
    varDispatch  : typeString := 'varDispatch';
    varError     : typeString := 'varError';
    varBoolean   : typeString := 'varBoolean';
    varVariant   : typeString := 'varVariant';
    varUnknown   : typeString := 'varUnknown';
    varByte      : typeString := 'varByte';
    varWord      : typeString := 'varWord';
    varLongWord  : typeString := 'varLongWord';
    varInt64     : typeString := 'varInt64';
    varStrArg    : typeString := 'varStrArg';
    varString    : typeString := 'varString';
    varAny       : typeString := 'varAny';
    varTypeMask  : typeString := 'varTypeMask';
  end;

  // Show the Variant type
  ShowMessage('Variant type is '+typeString);
end;
Show full unit code
   Variant value = not yet set
   Variant type  = varEmpty
   Variant value = 123
   Variant type  = varByte
   Variant value = 123 + 456
   Variant type  = varInt64
   Variant value = String 579
   Variant type  = varString