Difference between revisions of "As"

From Lazarus wiki
Jump to navigationJump to search
(rewrite)
Line 1: Line 1:
 
{{as}}
 
{{as}}
  
 +
The [[Operator|operator]] <syntaxhighlight lang="pascal" enclose="none">as</syntaxhighlight> performs a conditional [[Typecast|typecast]].
 +
The word <syntaxhighlight lang="pascal" enclose="none">as</syntaxhighlight> is a [[Reserved word|reserved word]] in [[Mode Delphi|<syntaxhighlight lang="pascal" enclose="none">{$mode Delphi}</syntaxhighlight>]] and [[Mode ObjFPC|<syntaxhighlight lang="pascal" enclose="none">{$mode objFPC}</syntaxhighlight>]].
  
The [[Reserved word|reserved word]] <syntaxhighlight lang="pascal" enclose="none">as</syntaxhighlight> is used to cast (use one variable type as if it were another type)
+
== operation ==
 +
<syntaxhighlight lang="pascal" enclose="none">as</syntaxhighlight> requires a [[Class|<syntaxhighlight lang="pascal" enclose="none">class</syntaxhighlight>]] or (COM) interface as the first argument and a class or interface reference as the second.
 +
The expression <syntaxhighlight lang="pascal" enclose="none">child as super</syntaxhighlight> is equivalent to the expression and statement:
 +
<syntaxhighlight lang="pascal">
 +
super(child)
 +
if not assigned(child) and_then not child is super then
 +
begin
 +
raise exception.create(sErrInvalidTypecast);
 +
end;
 +
</syntaxhighlight>
 +
{{Warning|Typecasting a [[Nil|<syntaxhighlight lang="pascal" enclose="none">nil</syntaxhighlight> pointer]] will not raise an exception.}}
 +
However, trying to de-reference <syntaxhighlight lang="pascal" enclose="none">nil</syntaxhighlight> by attempting to access an attribute or method ''will'' cause a [[runtime error|RTE]].
  
<syntaxhighlight>
+
== application ==
procedure TmainForm.selectionGridDrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
+
<syntaxhighlight lang="pascal" enclose="none">as</syntaxhighlight> ensures a typecast is legit.
begin
 
    (Sender As TDBGrid).DefaultDrawColumnCell(Rect,DataCol,Column,State);
 
end;
 
</syntaxhighlight>
 
  
here the Sender [[Variable|variable]] of [[Type|type]] [[TObject]] is being cast as type [[TDBGrid]]
+
<syntaxhighlight lang="pascal" enclose="none">as</syntaxhighlight> is one of the operators that can not be [[Operator overloading|overloaded]].
  
<syntaxhighlight lang="pascal" enclose="none">As</syntaxhighlight> is an [[Object Pascal]] and a Borland [[Delphi]] extension.
+
== see also ==
 +
* [[Is|<syntaxhighlight lang="pascal" enclose="none">is</syntaxhighlight>]]

Revision as of 10:27, 23 August 2019

Deutsch (de) English (en) español (es) suomi (fi) français (fr)

The operator as performs a conditional typecast. The word as is a reserved word in {$mode Delphi} and {$mode objFPC}.

operation

as requires a class or (COM) interface as the first argument and a class or interface reference as the second. The expression child as super is equivalent to the expression and statement:

	super(child)
	if not assigned(child) and_then not child is super then
	begin
		raise exception.create(sErrInvalidTypecast);
	end;
Warning-icon.png

Warning: Typecasting a nil pointer will not raise an exception.

However, trying to de-reference nil by attempting to access an attribute or method will cause a RTE.

application

as ensures a typecast is legit.

as is one of the operators that can not be overloaded.

see also