Link to home
Start Free TrialLog in
Avatar of __alex
__alex

asked on

The as operator

I frequently use the is operator but never the as operator and wonder what it is good for. Everything can be done with a simple cast...

procedure Foo(o: TObject);
begin
  if o is TMyObject then
    TMyObject(o).DoStuff;
  ...
end;

Opinions?
SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
from delphi help:

The as operator performs checked typecasts. The expression

object as class

returns a reference to the same object as object, but with the type given by class. At runtime, object must be an instance of the class denoted by class or one of its descendants, or be nil; otherwise an exception is raised. If the declared type of object is unrelated to class
--that is, if the types are distinct and one is not an ancestor of the other--a compilation error results.

meikl ;-)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of __alex
__alex

ASKER

@kretzschmar
Why check things twice:
if o is TMyObject then             // First check
  (o as TMyObject).DoStuff;     // Second check ???

@mokule
Good point but...
if you know b is a memo, why do you want to set its caption?
If you don't know what b is it's better to check it or an exception might be raised:
  if b is TLabel then TLabel(b).Caption := foo;

Still can't think of any (real) use of the as
Avatar of __alex

ASKER

@geobul -> @kretzschmar
:-)) see my first comment,

this is the difference,
the compiler developers cannot know,
if you check it before with the is-operater,
thats why the as-operator is introduced
Avatar of __alex

ASKER

@kretzschmar
Ok, now I get your point, thanks!
maybe this illustrates the difference,
watch about the different exceptions u get

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    procedure AsTypeCastClick(Sender: TObject);
    procedure HardTypeCastClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure ShowItems(AComboBox : TComboBox);
begin
  if assigned(AComboBox) then
    ShowMessage(AComboBox.Items.Text);
end;

//is assigned to Button1 and ComboBox1 onClick-event
procedure TForm1.AsTypeCastClick(Sender: TObject);
begin
  ShowItems(sender as TComboBox);
end;

//is assigned to Button2 and ComboBox2 onClick-event
procedure TForm1.HardTypeCastClick(Sender: TObject);
begin
  ShowItems(TComboBox(sender));
end;

end.

meikl ;-)
>(o as TMyObject).DoStuff;     // Second check ???

It's not a check but typecast
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of __alex

ASKER

Very nice! It makes the code look pretty...