Name

$VarStringChecks Compiler Directive

Syntax

{$V+}                 // default
{$VarStringChecks On} // default
{$V-}
{$VarStringChecks Off}

Scope

Local

Description

Ordinarily, the type of an argument for a var parameter must match the parameter’s type exactly. This restriction ensures type safety when the subroutine modifies the var parameter. If the $VarStringChecks directive is disabled, Delphi loosens the restriction for short strings, letting you pass any short string argument to a subroutine with any type of var short string parameter.

If you disable $VarStringChecks, you must take care that the subroutine does not exceed the size of the short string argument. One way to ensure this is to pass only arguments of maximum possible length (e.g., string[255] or ShortString types). Even better is to use long strings instead of short strings.

Tips and Tricks

Disable $VarStringChecks only when needed for backward compatibility. In new code, you should usually use long strings, but if you must use short strings, try using type OpenString instead of disabling $VarStringChecks.

See Also

AnsiString Type, OpenString Type, ShortString Type, String Keyword

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.