Name

VarCast Procedure

Syntax

procedure VarCast(var Dest: Variant; const Source: Variant;
                  VarType: Integer);
procedure VarCast(var Dest: OleVariant; const Source: Variant;
                  VarType: Integer);

Description

VarCast performs a typecast of Source to a new Variant type, storing the result in Dest. VarType must not contain the varArray or varByRef bits.

If Dest is an OleVariant, the new type must be an OLE-compatible type. In particular, you cannot cast to varString, but must use varOleStr instead. Otherwise, VarCast reports runtime error 15 (EVariantError).

VarCast is not a real procedure.

Tips and Tricks

  • VarCast is similar to VarAsType. The difference is that VarCast performs additional tests when casting to an OleVariant.

  • If the type of Source is already VarType, VarCast performs a simple copy.

Example

var
  V: Variant;
  O: OleVariant;
begin
  V := 'This is a test';     // VarType(V) = varString
  VarCast(O, V, varOleStr);

See Also

OleVariant Type, TVarData Type, VarAsType Function, VarCopy Procedure, Variant Type, VarIsArray Function, VarIsEmpty Function, VarIsNull Function

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.