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 a Variant must be checked at runtime, which can be costly if your program often uses Variants.

  • Variants are usually easy to understand and use, but they have some subtleties. For example, Unassigned and Null represent distinctly different Variant values and concepts. One way to think of the difference is to imagine a Variant as a box that contains a piece of paper. On the paper is written the Variant’s value: a number, a string, the time of day, etc. Null is a blank piece of paper. Unassigned is an empty box.

  • To convert a TDateTime to or from a Variant, you must use the functions VarFromDateTime and VarToDateTime.

  • The most common use for Variants is calling an OLE automation server when you don’t have a dispinterface at compile time. You call the server’s methods, but Delphi cannot look up the methods at compile time, so it checks them at runtime. Ordinarily, you could not call a method without having a base class or interface to declare the method’s name, arguments, and return type, but using a Variant, you can defer these details until runtime, when Delphi uses the server’s IDispatch interface to look up the method and its signature.

Example

var
  WordApp: Variant;
  A, X: Variant;
begin
  WordApp := CreateOleObject('Word.Basic'),
  WordApp.FileNew;

  X := Pi;
  X := X / 2.0;   // Mix Variants and numbers in expressions.
  ShowMessage(X); // Delphi automatically converts to a string.

  // Create a 3x3 matrix.
  A := VarArrayCreate([1, 3, 1, 3], varDouble);
  for I := 1 to 3 do
    for J := 1 to 3 do
      A[I, J] := I + J;
end;

See Also

ChangeAnyProc Variable, ClearAnyProc Variable, EmptyParam Variable, Null Variable, OleVariant Type, RefAnyProc Variable, TVarData Type, Unassigned Variable, VarArrayCreate Function, VarArrayDimCount Function, VarArrayHighBound Function, VarArrayLock Function, VarArrayLowBound Function, VarArrayOf Function, VarArrayRedim Procedure, VarArrayRef Function, VarArrayUnlock Procedure, VarAsType Function, VarCast Procedure, VarClear Procedure, VarCopy Procedure, VarDispProc Variable, VarFromDateTime Function, VarIsArray Function, VarIsEmpty Function, VarIsNull Function, VarToDateTime Function, VarToStr Function, VarType Function
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset