Difference between revisions of "As"

From Lazarus wiki
Jump to navigationJump to search
(What does extension mean? Please check but presumably Object Pascal is meant?!)
 
(6 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 +
{{as}}
  
== As ==
+
The [[Operator|operator]] <syntaxhighlight lang="pascal" inline>as</syntaxhighlight> performs a conditional [[Typecast|typecast]].
 +
The word <syntaxhighlight lang="pascal" inline>as</syntaxhighlight> is a [[Reserved word|reserved word]] in [[Mode Delphi|<syntaxhighlight lang="pascal" inline>{$mode Delphi}</syntaxhighlight>]] and [[Mode ObjFPC|<syntaxhighlight lang="pascal" inline>{$mode objFPC}</syntaxhighlight>]].
  
 +
== operation ==
 +
<syntaxhighlight lang="pascal" inline>as</syntaxhighlight> requires a [[Class|<syntaxhighlight lang="pascal" inline>class</syntaxhighlight>]] or (COM) interface as the first argument and a class or interface reference as the second.
 +
The expression <syntaxhighlight lang="pascal" inline>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" inline>nil</syntaxhighlight> pointer]] will not raise an exception.}}
 +
However, trying to de-reference <syntaxhighlight lang="pascal" inline>nil</syntaxhighlight> by attempting to access an attribute or method ''will'' cause a [[runtime error|RTE]].
  
The [[Keyword|keyword]] '''as''' is used to cast (use one variable type as if it were another type)
+
== application ==
 
+
<syntaxhighlight lang="pascal" inline>as</syntaxhighlight> ensures a typecast is legit.
<syntaxhighlight>
 
procedure TmainForm.selectionGridDrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
 
begin
 
    (Sender As TDBGrid).DefaultDrawColumnCell(Rect,DataCol,Column,State);
 
end;
 
</syntaxhighlight>
 
  
here the Sender variable of type TObject is being cast as type TDBGrid
+
<syntaxhighlight lang="pascal" inline>as</syntaxhighlight> is one of the operators that can not be [[Operator overloading|overloaded]].
  
As is an Object Pascal language extension
+
== see also ==
 +
* [[Is|<syntaxhighlight lang="pascal" inline>is</syntaxhighlight>]]

Latest revision as of 17:23, 6 August 2022

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