# Unit Snippets

A unit snippet defines a complete Pascal unit. If a unit compiles successfully in Delphi or in Free Pascal then it is valid for use as a unit snippet.

Unit snippets are compilable just like all the other snippet kinds, except freeform. However, that is where the similarities end. Specifically, unit snippets:

- Can't be referenced by other snippets except as cross references.
- Can't depend on any other snippets.
- Can't be included in a generated unit. This is obvious: units can't contain other units.
- Must be self contained. Any constants, types (including classes and advanced records) or routines referenced by the unit's code must be defined in the unit itself on in a unit included in one the unit's uses statements†. To compile correctly the used units must be accessible to the available compilers (i.e. be on the compiler's search path‡.)
- Do not have comments that contain the snippet description inserted into the source code when copied or printed.

Here is an example of a unit snippet:

    // A sample unit, adapted from the CodeSnip source code repository

    unit UShowCaseCtrl;

    interface

    uses
      Controls;

    type
      // Simple transparent control designed to be placed over other controls to
      // prevent user interaction with the "show cased" controls.
      TShowCaseCtrl = class(TCustomControl)
      protected
        // Modifies window creation parameters. Ensures control is transparent.
        procedure CreateParams(var Params: TCreateParams); override;
      end;

    implementation

    uses
      Windows;

    { TShowCaseCtrl }

    procedure TShowCaseCtrl.CreateParams(var Params: TCreateParams);
    begin
      inherited;
      Params.ExStyle := Params.ExStyle or WS_EX_TRANSPARENT;
    end;

    end.

In this unit, the Windows and Controls units must be accessible to the available compilers. All is well in this case because the units are in the VCL.

Notes

† For other compilable snippet types the required units are entered in the Snippets Editor, but for unit snippets they are entered in the source code itself.

‡ Compiler search paths can be configured in the Search Paths tab of the Configure Compilers dialogue box.