Name
Variant Type
Syntax
type Variant;
Description
The Variant
type is a dynamic type. A
Variant variable can change type at runtime,
sometimes storing an integer, other times a string, and other times
an array.
Delphi automatically casts numbers, strings, interfaces, and other
types to and from Variants as needed. You can also
use a number of functions to further manipulate and work with
Variants.
Chapter 6, lists all the possible types for a
Variant.
Tips and Tricks
Variants offer flexibility, but you pay a performance price. Even simple arithmetic with
Variants is much more complicated and time-intensive than arithmetic with typed variables. Every reference to aVariantmust be checked at runtime, which can be costly if your program often usesVariants.Variants are usually easy to understand and use, but they have some subtleties. For example,UnassignedandNullrepresent distinctly differentVariantvalues and concepts. One way to think of the difference is to imagine aVariantas a box that contains a piece of paper. On the paper is written theVariant’s value: a number, a string, the time of day, etc.Nullis a blank piece of paper.Unassignedis an empty box.To convert a
TDateTimeto or from aVariant, you must use the functionsVarFromDateTimeandVarToDateTime.The most common use for
Variants is calling an OLE automation server when you don’t have adispinterfaceat compile time. You call the server’s methods, but Delphi cannot look up the methods at compile time, so it checks ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access