Name

OpenString Type

Syntax

Subroutine declaration(...; var Param: OpenString; ...);

Description

OpenString declarations are let you pass a ShortString argument to a subroutine when the subroutine does not know the exact string size. Ordinarily, a var string parameter’s type must match exactly with an actual argument’s type. In the case of short strings, the maximum string length is part of the type and must match, to ensure that the subroutine does not try to store more characters in the string than can fit.

An OpenString parameter relaxes the restriction and lets you pass any size string to the subroutine. Delphi passes an additional, hidden parameter that contains the maximum string size. References to Param in the subroutine, therefore, are type safe.

Tips and Tricks

  • The $OpenStrings compiler directive makes var ShortString parameters behave as OpenString parameters. This directive is enabled by default. The only time you need to declare a parameter as type OpenString is when you are disabling the $OpenStrings compiler directive.

  • If you disable the $LongStrings compiler directive, the string type is a short string, so var string parameters are like OpenString (unless you also disable the $OpenStrings compiler directive).

  • If you are using long strings, you have no need for OpenString parameters.

See Also

AnsiString Type, ShortString Type, String Keyword, Var Keyword, $H Compiler Directive, $LongStrings Compiler Directive, $OpenStrings Compiler Directive, $P Compiler Directive

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.