From RAD Studio API Documentation
Jump to: navigation, search


type Variant = { built-in type };


class RTL_DELPHIRETURN Variant: public TVarData


Type Visibility Source Unit Parent
System System


Used for variables that can change type at run time.

The Variant type is used for variables that can change type at run time. Variants offer greater flexibility, but consume more memory than regular variables, and operations on them are slower than on statically bound types. Moreover, illicit operations on variants often result in run-time errors, where similar mistakes with regular variables would have been caught at compile time. You can also create custom variant types.

By default, Variant variables can hold values of any type except records, sets, static arrays, files, classes, class references, and pointers. In other words, variants can hold anything but structured types and pointers. They can hold interfaces, whose methods and properties can be accessed through them. They can hold dynamic arrays, and they can hold a special kind of static array called a variant array. Variants can mix with other variants and with Integer, Real, string, and Boolean values in expressions and assignments; the compiler automatically performs type conversions.

Variants that contain strings cannot be indexed.

You can define custom variants that extend the Variant type to hold arbitrary values. For example, you can define a Variant string type that allows indexing or that holds a particular class reference, record type, or static array. Custom Variant types are defined by creating descendants to the TCustomVariantType class.

On 32-bit platforms, a Variant occupies 16 bytes of memory. On 64-bit platforms, a Variant is a 24-byte record. A variant record consists of a type code and a value, or a pointer to a value, of the type specified by the type code. See Internal Representation of Variant Types. All variants are initialized on creation to the special value Unassigned. The special value Null indicates unknown or missing data.

See Also

Code Examples