__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?
procedure Foo(o: TObject);
begin
if o is TMyObject then
TMyObject(o).DoStuff;
...
end;
Opinions?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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
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
ASKER
@kretzschmar
Ok, now I get your point, thanks!
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.Item s.Text);
end;
//is assigned to Button1 and ComboBox1 onClick-event
procedure TForm1.AsTypeCastClick(Sen der: TObject);
begin
ShowItems(sender as TComboBox);
end;
//is assigned to Button2 and ComboBox2 onClick-event
procedure TForm1.HardTypeCastClick(S ender: TObject);
begin
ShowItems(TComboBox(sender ));
end;
end.
meikl ;-)
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.Item
end;
//is assigned to Button1 and ComboBox1 onClick-event
procedure TForm1.AsTypeCastClick(Sen
begin
ShowItems(sender as TComboBox);
end;
//is assigned to Button2 and ComboBox2 onClick-event
procedure TForm1.HardTypeCastClick(S
begin
ShowItems(TComboBox(sender
end;
end.
meikl ;-)
>(o as TMyObject).DoStuff; // Second check ???
It's not a check but typecast
It's not a check but typecast
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Very nice! It makes the code look pretty...
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 ;-)