Nested Type Declarations

From RAD Studio
Jump to: navigation, search

Go Up to Classes and Objects Index

Type declarations can be nested within class declarations. Nested types are used throughout object-oriented programming in general. They present a way to keep conceptually related types together, and to avoid name collisions. The same syntax for declaring nested types may be used with the Win32 Delphi compiler.

Declaring Nested Types

The nestedTypeDeclaration follows the type declaration syntax defined in Declaring Types.


type
  className = class [abstract | sealed] (ancestorType)
      memberList

      type
         nestedTypeDeclaration

      memberList
  end;

Nested type declarations are terminated by the first occurrence of a non-identifier token, for example, procedure, class, type, and all visibility scope specifiers.

The normal accessibility rules apply to nested types and their containing types. A nested type can access an instance variable (field, property, or method) of its container class, but it must have an object reference to do so. A nested type can access class fields, class properties, and class static methods without an object reference, but the normal Delphi visibility rules apply.

Nested types do not increase the size of the containing class. Creating an instance of the containing class does not also create an instance of a nested type. Nested types are associated with their containing classes only by the context of their declaration.

Declaring and Accessing Nested Classes

The following example demonstrates how to declare and access fields and methods of a nested class:

type
      TOuterClass = class
       strict private
          myField: Integer;

       public
          type
             TInnerClass = class
              public
                myInnerField: Integer;
                procedure innerProc;
             end;

         procedure outerProc;
       end;

To implement the innerProc method of the inner class, you must qualify its name with the name of the outer class. For example:

procedure TOuterClass.TInnerClass.innerProc;
begin
   ...
end;

To access the members of the nested type, use dotted notation as with regular class member access. For example:

var
   x: TOuterClass;
   y: TOuterClass.TInnerClass;

begin
   x := TOuterClass.Create;
   x.outerProc;
   ...
   y := TOuterClass.TInnerClass.Create;
   y.innerProc;

Nested Constants

Constants can be declared in class types in the same manner as nested type sections. Constant sections are terminated by the same tokens as nested type sections, specifically, reserved words or visibility specifiers. Typed constants are not supported, so you cannot declare nested constants of value types, such as System.Currency, or System.TDateTime.

Nested constants can be of any simple type: ordinal, ordinal subranges, enums, strings, and real types.

The following code demonstrates the declaration of nested constants:

type
   TMyClass = class
        const
             x = 12;
             y = TMyClass.x + 23;
        procedure Hello;
        private
             const
                 s = 'A string constant';
    end;

begin
   Writeln(TMyClass.y);   // Writes the value of y, 35.
end.

See Also