Variant Type Conversions

All integer, real, string, character, and Boolean types are assignment-compatible with Variant. Expressions can be explicitly cast as variants, and the VarAsType and VarCast standard routines can be used to change the internal representation of a variant. The following code demonstrates the use of variants and some of the automatic conversions performed when variants are mixed with other types.

V1, V2, V3, V4, V5: Variant; I: Integer; D: Double; S: string;



= 1; {

integer value }


= 1234.5678; {


value }


= 'Hello world!

'; {

string value }


= '1000';


string value }


= V1 + V2 + V4;


real value 2235.5678}


= V1;


I = 1 (integer value) }


= V2;


D = 1234.5678 (real value)



= V3;


S = 'Hello world!' (string



= V4;


I = 1000 (integer value) }


= V5;


S = '2235.5678' (string value) }


The compiler performs type conversions according to the following rules. Variant type conversion rules


integer real





converts integer formats converts to real

converts to string representation

returns False if 0, True otherwise


rounds to nearest integerconverts real formats

converts to string representation using regional settings

returns False if 0, True otherwise


converts to integer, converts to real using converts string/character truncating if necessary; regional settings; raisesformats raises exception if string exception if string is not is not numeric numeric

returns False if string is 'false' (noncase-sensitive) or a numeric string that evaluates to 0, True if string is 'true' or a nonzero numeric string; raises exception otherwise


same as string (above) same as string (above) same as string (above)

same as string (above)


False = 0, True = 1 (255 False = 0, True = 1 if Byte)

False = '0', True = '1'

False = False, True = True

Unassignedreturns 0 returns 0

returns empty string

returns False


raises exception raises exception

raises exception

raises exception

Out-of-range assignments often result in the target variable getting the highest value in its range. Invalid variant operations, assignments or casts raise an EVariantError exception or an exception class decending from EVariantError.

Special conversion rules apply to the TDateTime type declared in the System unit. When a TDateTime is converted to any other type, it treated as a normal Double. When an integer, real, or Boolean is converted to a TDateTime, it is first converted to a Double, then read as a date-time value. When a string is converted to a TDateTime, it is interpreted as a date-time value using the regional settings. When an Unassigned value is converted to TDateTime, it is treated like the real or integer value 0. Converting a Null value to TDateTime raises an exception.

On the Win32 platform, if a variant references a COM interface, any attempt to convert it reads the object's default property and converts that value to the requested type. If the object has no default property, an exception is raised.

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment