# Class & Advanced Record Snippets

A snippet of the class & advanced record kind defines one or more Pascal classes or advanced records (i.e. records with methods).

The source code must conform to the following rules:

- Each snippet must begin with the type keyword as the first non-white-space text in the source code. Comments must not precede it. The type keyword must be followed by at least one space or a new line.
- Next follows the class or advance record declaration(s). Additional type keywords may be used between types, but are not required.
- Method implementations follow once all the class / record types have been declared. Note that an implementation must be provided for all classes and record types.
- Unlike in a Pascal unit, the implementation keyword must not be used to separate the declaration and the implementation: CodeSnip works out where the implementation starts.

Warning: It is an error to use this snippet kind for records or classes that have no method implementations (for example pure abstract classes) – use a simple type definition snippet instead.

Here are examples valid class and advanced record snippets:

Example 1

    type
      TMyClass = class(TObject)
      private
        fField: string;
      public
        constructor Create(AFoo: string);
        function Foo: string;
      end;

    constructor TMyClass.Create(AFoo: string);
    begin
      inherited Create;
      fField := AFoo;
    end;

    function TMyClass.Foo: string;
    begin
      Result := fField;
    end;

Example 2

    type
      TPointEx = record
        X, Y: Integer;
        constructor Create(AX, AY: Integer);
      end;

    type // optional type keyword
      TRectEx = record
        Top, Left, Width, Height: Integer;
        constructor Create(ATop, ALeft, AWidth, AHeight: Integer);
      end;

    constructor TPointEx.Create(AX, AY: Integer);
    begin
      X := AX;
      Y := AY;
    end;

    constructor TRectEx.Create(ATop, ALeft, AWidth, AHeight: Integer);
    begin
      Top := ATop;
      Left := ALeft;
      Width := AWidth;
      Height := AHeight;
    end;

Class & Advanced Record snippets may refer to other class & advanced record snippets, routines, simple type definitions and constants, providing they are defined in Delphi units or elsewhere in the database. The snippet's units and dependency references should indicate where to find the referenced snippets. Freeform and unit snippets may not be referenced.