Marco Web Center

[an error occurred while processing this directive]

Home: Code Repository: Delphi 2009 Handbook

Project: VariantOver.dproj

Project Structure

VariantOver.dpr
program VariantOver;

uses
  Forms,
  VariantOver_MainForm in 'VariantOver_MainForm.pas' {FormVariantOver};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TFormVariantOver, FormVariantOver);
  Application.Run;
end.
VariantOver_MainForm.pas
unit VariantOver_MainForm;

interface

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

type
  TMyRecord = record
  private
    X: Integer;
  public
    class operator Implicit(const Value: Variant): TMyRecord;
  end;

  TFormVariantOver = class(TForm)
    btnAmbigousVariant: TButton;
    btnInterface: TButton;
    btnRecordFromVariant: TButton;
    Memo1: TMemo;
    procedure btnAmbigousVariantClick(Sender: TObject);
    procedure btnInterfaceClick(Sender: TObject);
    procedure btnRecordFromVariantClick(Sender: TObject);
  private
    procedure Log (const msg: String);

    procedure ShowValue(I: Integer); overload;
    procedure ShowValue(s: string); overload;

    procedure ShowValue2(const Value: IInterface); overload;
    procedure ShowValue2(Value: TObject); overload;

    procedure ShowValue3(const R: TMyRecord); overload;
    procedure ShowValue3(X: Integer); overload;
  public
    { Public declarations }
  end;

var
  FormVariantOver: TFormVariantOver;

implementation

{$R *.dfm}

procedure TFormVariantOver.ShowValue (I: Integer);
begin
  Log ('The integer value is ' + IntToStr (I));
end;

procedure TFormVariantOver.ShowValue (s: string);
begin
  Log ('The string value is ' + s);
end;

//procedure ShowValue (v: variant); overload;
//begin
//  Log ('The variant value is ' + string(v));
//end;

procedure TFormVariantOver.btnAmbigousVariantClick(Sender: TObject);
var
  v: variant;
begin
  v := 3;
  // ShowValue (v); // compiler error in Delphi 2009, works in Delphi 2007
  ShowValue (Integer(v)); // works in Delphi 2009
  v := 'foo';
  // ShowValue (v); // compiler error in Delphi 2009, runtime error in Delphi 2007
  ShowValue (string(v)); // works in Delphi 2009
end;

procedure TFormVariantOver.ShowValue2(Value: TObject);
begin
  Log('TObject');
end;

procedure TFormVariantOver.ShowValue2(const Value: IInterface);
begin
  Log('IInterface');
end;

procedure TFormVariantOver.btnInterfaceClick(Sender: TObject);
var
  anObj: TInterfacedObject;
begin
  // same effect in both Delphi 2009 and Delphi 2007
  anObj := TInterfacedObject.Create;
  ShowValue2 (anObj);
end;

class operator TMyRecord.Implicit(const Value: Variant): TMyRecord;
begin
  Result.X := Value;
end;

procedure TFormVariantOver.ShowValue3 (X: Integer);
begin
  Log ('Integer: ' + IntToStr(X));
end;

procedure TFormVariantOver.ShowValue3 (const R: TMyRecord);
begin
  Log ('TMyRecord: ' + IntToStr(R.X));
end;

procedure TFormVariantOver.btnRecordFromVariantClick(Sender: TObject);
var
  v: Variant;
begin
  v := 10;
  ShowValue3 (v);
end;

procedure TFormVariantOver.Log (const msg: String);
begin
  Memo1.Lines.Add(msg);
end;

end.
VariantOver_MainForm.pas.dfm
object FormVariantOver: TFormVariantOver
  Left = 0
  Top = 0
  Caption = 'VariantOver'
  ClientHeight = 259
  ClientWidth = 613
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object btnAmbigousVariant: TButton
    Left = 16
    Top = 16
    Width = 129
    Height = 25
    Caption = 'btnAmbigousVariant'
    TabOrder = 0
    OnClick = btnAmbigousVariantClick
  end
  object btnInterface: TButton
    Left = 16
    Top = 56
    Width = 129
    Height = 25
    Caption = 'btnInterface'
    TabOrder = 1
    OnClick = btnInterfaceClick
  end
  object btnRecordFromVariant: TButton
    Left = 16
    Top = 96
    Width = 129
    Height = 25
    Caption = 'btnRecordFromVariant'
    TabOrder = 2
    OnClick = btnRecordFromVariantClick
  end
  object Memo1: TMemo
    Left = 160
    Top = 16
    Width = 433
    Height = 225
    TabOrder = 3
  end
end
HTML file generated by PasToWeb, a tool by Marco Cantù
Copyright 2008 Marco Cantù