Mega Code Archive

 
Categories / Delphi / Examples
 

How to find out which type my variant currently is

Title: How to find out which type my variant currently is? Question: A variant can have most any kind of value assigned to it. How can we tell which it is? Answer: There is a value stored within the variant itself that specifies which type it is by value. This function does it: function GetVariantType(const v: variant): string; begin case TVarData(v).vType 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'; varString: result := 'String'; varTypeMask: result := 'TypeMask'; varArray: result := 'Array'; varByRef: result := 'ByRef'; end; // case end;