DelphiBasics
$H
Compiler Directive
Treat string types as AnsiString or ShortString
{$H-}
1
2 {$H+}
Description
The $H compiler directive determines whether Delphi treats the string type as an AnsiString when on (default) or ShortString when off.
 
The default recognises that the general use of string types is to hold data that is likely to be longer than 255 characters (ShortString capacity).
Notes
$LongStrings is equivalent to $H.

$H can be used multiple times in your code, but this is not recommended (except for illustration purposes in the example).

The default value is $H+
Related commands
$LongStringsTreat string types as AnsiString or ShortString
AnsiStringA data type that holds a string of AnsiChars
ShortStringDefines a string of up to 255 characters
StringA data type that holds a string of characters
 Download this web site as a Windows program.




 
Example code : Creating two types of string in the same code
var
  // Define littleString to be treated as a ShortString
  {$H-}
  littleString : string;

  // Define bigString to be treated as an AnsiString
  {$H+}
  bigString : string;
begin
  // Show the size of the little string - 256 bytes
  ShowMessageFmt('littleString size = %d',[SizeOf(littleString)]);

  // Show the size of the big string - 4 - a pointer to text
  ShowMessageFmt('   bigString size = %d',[SizeOf(bigString)]);
end;
Show full unit code
  littleString size = 256
   bigString size = 4
 
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page