Search code examples
delphidelphi-2009rttitypeinfo

String representation of the content type of a Variant?


First, apologies for my English, I hope it makes sense what I`ve written here. Now to my problem.

How can I get the string representation of the content type of a Variant using TypInfo.GetEnumName(). I have tried the following, without luck, I get a numeric representation.

myString := GetEnumName( TypeInfo(TVarType), TVarData(myVar).VType );

Thank you.


Solution

  • Just use the build-in Delphi function for getting the string representation of a Variant type.

    var
      MyVariantType: string;
      MyVariant: Variant;
    begin
      MyVariant := 'Hello World';
      MyVariantType := VarTypeAsText(VarType(MyVariant));
      ShowMessage(MyVariantType); //displays: String
    
      MyVariant := 2;
      MyVariantType := VarTypeAsText(VarType(MyVariant));
      ShowMessage(MyVariantType); //displays: Byte
    end;