8.7.7 Class operators

Class operators are slightly dierent from the operators above in the sense that they can only be used in class expressions which return a class. There are only 2 class operators, as can be seen in table (8.8).


Table 8.8: Class operators

OperatorAction
is Checks class type
as Conditional typecast

An expression containing the is operator results in a boolean type. The is operator can only be used with a class reference or a class instance. The usage of this operator is as follows:

 Object is Class

This expression is completely equivalent to

 Object.InheritsFrom(Class)

If Object is Nil, False will be returned.

The following are examples:

Var  
  A : TObject;  
  B : TClass;  
 
begin  
  if A is TComponent then ;  
  If A is B then;  
end;

The as operator performs a conditional typecast. It results in an expression that has the type of the class:

  Object as Class

This is equivalent to the following statements:

  If Object=Nil then  
    Result:=Nil  
  else if Object is Class then  
    Result:=Class(Object)  
  else  
    Raise Exception.Create(SErrInvalidTypeCast);

Note that if the object is nil, the as operator does not generate an exception.

The following are some examples of the use of the as operator:

Var  
  C : TComponent;  
  O : TObject;  
 
begin  
  (C as TEdit).Text:='Some text';  
  C:=O as TComponent;  
end;

Found a problem.

The following works fine:

test10a.pp
----------------------------------------------------------------------
unit test10a;

interface

const
  a = 'a';
  b = 'b';

implementation

begin
end.
----------------------------------------------------------------------

test10b.pp
----------------------------------------------------------------------
program test;

uses
  test10a;

var
  c : Char;

begin
  Write( 'A, or B: ' );
  Readln( c );
  case c of
    a ,
    b : Writeln( 'ok:  ' + c );
    else
      Writeln( 'oops ' + c );
  end;
end.
----------------------------------------------------------------------

This, however, fails.

test11a.pp
----------------------------------------------------------------------
unit test11a;

interface

const
  a : char = 'a';
  b : char = 'b';

implementation

begin
end.
----------------------------------------------------------------------

test11b.pp
----------------------------------------------------------------------
program test11b;

uses
  test11a;

var
  c : Char;

begin
  Write( 'A, or B: ' );
  Readln( c );
  case c of
    a ,
    b : Writeln( 'ok:  ' + c );
    else
      Writeln( 'oops ' + c );
  end;
end.
----------------------------------------------------------------------

  The only difference being the type identification of the char constants.

Bob

-- Bob Gibson on December 21, 2006 01:54 AM (view details)

simplier failure

program test12;

const
  a : char = 'a';
  b : char = 'b';

var
  c : Char;

begin
  Write( 'A, or B: ' );
  Readln( c );
  case c of
    a ,
    b : Writeln( 'ok:  ' + c );
    else
      Writeln( 'oops ' + c );
  end;
end.

-- Bob Gibson on December 21, 2006 02:01 AM (view details)

Statement-List in "else" part

pharmacy; http://wpdesigner.com/forums/showthread.php?p=80153#post80153 buy cheap acomplia,; http://www.searchles.com/groups/show/Effexor+%3E%3E+Buy+online+%3E%3E+Fast+%26+Secure+%3E%3E+worldwide+delivery effexor; http://www.imeem.com/people/P9XVsO1/blogs/2008/07/23/U9g-a_rP/yohimbe_good_prices_assortment_usa_worldwide_delivery yohimbe; http://www.searchles.com/groups/show/Xanax.+Buy+online+%3E%3E+Fast+%26+Secure+%3E%3E+worldwide+delivery purchase xanax online; http://www.searchles.com/groups/show/Levitra.+Buy+online+%3E%3E+Fast+%26+Secure+%3E%3E+worldwide+delivery buy levitra online; http://www.searchles.com/groups/show/Acomplia.+Buy+online+%3E%3E+Fast+%26+Secure+%3E%3E+worldwide+delivery purchase acomplia; http://www.searchles.com/groups/show/Prozac.+Buy+online+%3E%3E+Fast+%26+Secure+%3E%3E+worldwide+delivery generic prozac; http://www.imeem.com/people/P9XVsO1/blogs/2008/07/23/qbI3cmJA/cardura_good_prices_assortment_usa_worldwide_delivery cardura; http://vienna.de.craigslist.at/laf/765037857.html rimonabant; http://vienna.de.craigslist.at/laf/765043976.html soma; http://www.searchles.com/groups/show/Rimonabant.+Buy+online+%3E%3E+Fast+%26+Secure+%3E%3E+worldwide+delivery rimonabant; http://wpdesigner.com/forums/showthread.php?p=80013#post80013 order cialis online,; http://www.imeem.com/people/P9XVsO1/blogs/2008/07/23/FjI8DsQU/cialis_professional_good_prices_assortment_usa_worldwide_delivery cialis professional; http://www.imeem.com/people/P9XVsO1/blogs/2008/07/23/Q6oS8rfj/casodex_good_prices_assortment_usa_worldwide_delivery casodex; http://www.searchles.com/groups/show/Cialis.+Buy+online+%3E%3E+Fast+%26+Secure+%3E%3E+worldwide+delivery buy cialis,; http://www.searchles.com/groups/show/Buy+Viagra.+Buy+online+%3E%3E+Fast+%26+Secure+%3E%3E+worldwide+delivery buy viagra; http://wpdesigner.com/forums/showthread.php?p=81069#post81069 avodart; http://www.searchles.com/groups/show/Paxil+%3E%3E+Buy+online+%3E%3E+Fast+%26+Secure+%3E%3E+worldwide+delivery buy paxil; http://www.searchles.com/groups/show/Lexapro.+Buy+online+%3E%3E+Fast+%26+Secure+%3E%3E+worldwide+delivery order lexapro,; http://www.searchles.com/groups/show/Buy+Prozac.+Buy+online+%3E%3E+Fast+%26+Secure+%3E%3E+worldwide+delivery buy prozac; http://wpdesigner.com/forums/showthread.php?p=80166#post80166 purchase xanax; http://vienna.de.craigslist.at/laf/765043609.html tramadol online;

-- Unregistered Visitor on April 22, 2007 06:10 PM (view details)