OpenString Type
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.
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.