unit myTVarData;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

// BeginExample: SystemVarClear
// Routine: System.VarClear
// Type: System.TVarData

// BeginCode
{{
This example shows how to use the TVarData type in typecasts
of Variant variables to access the internals of a variable.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  V: Variant;
  mystr: string;
  MyBuffer: PChar;
begin
  if TVarData(V).VType = varEmpty then
    ListBox1.Items.Add('Variants start empty.');
  VarClear(V);
  TVarData(V).VType := varString;
  mystr := 'Here is my string';
  MyBuffer:= GetMemory(Length(mystr));
  StrCopy(MyBuffer, PChar(mystr));
  TVarData(V).VString := MyBuffer;
  ListBox1.Items.Add('This variant is now a string: ' + PChar(TVarData(V).VString));
  VarClear(V);
  TVarData(V).VType := varInteger;
  TVarData(V).VInteger := 1234567;
  ListBox1.Items.Add('This variant is now an integer: ' + IntToStr(TVarData(V).VInteger));
end;
// EndCode
// EndExample: SystemVarClear
end.
