6.8 Nested types, constants and variables

A class definition can contain a type section, const section and a variable section. The type and constant sections act as a regular type section as found in a unit or method/function/procedure implementation. The variables act as regular fields of the class, unless they are in a class var section, in which case they act as if they were defined at the unit level, within the namespace of the class (section 6.2, page 285).

However, the visibility of these sections does play a role: private and protected (strict or not) constants, types and variables can only be used as far as their visibility allows.

Public types can be used outside the class, by their full name:

type  
  TA = Class(TObject)  
  Public  
    Type TEnum = (a,b,c);  
    Class Function DoSomething : TEnum;  
  end;  
 
Class Function TA.DoSomething : TEnum;  
 
begin  
  Result:=a;  
end;  
 
var  
  E : TA.TEnum;  
 
begin  
  E:=TA.DoSomething;  
end.

Whereas

type  
  TA = Class(TObject)  
  Strict Private  
    Type TEnum = (a,b,c);  
  Public  
    Class Function DoSomething : TEnum;  
  end;  
 
Class Function TA.DoSomething : TEnum;  
 
begin  
  Result:=a;  
end;  
 
var  
  E : TA.TEnum;  
 
begin  
  E:=TA.DoSomething;  
end.

Will not compile and will return an error:

tt.pp(20,10) Error: identifier idents no member "TEnum"