Search code examples
delphipascal

Delphi 11 Define String Type ( ) vs [ ]


I'm using Delphi 11 to build TurboPower BTree Filer, which is supposed to support Delphi up to 2006 version. Since using newer version I have to convert Char to AnsiChar and String to AnsiString but this string type definition fails in Delphi 11:

const
  IsamFileNameLen = 64;

type
  IsamFileName = ansistring[IsamFileNameLen];

It doesn't like the square brackets, so I presume it wants parentheses (), but it didn't like those either?

What is the problem?

Edit: If it's just "string" then it is okay but it's not what I need, I need ansistring ?

Thanks!


Solution

  • It is true that string typically refers to AnsiString before D2009, and to UnicodeString since D2009. But, only when used as a dynamic long string . When used as a fixed-length short string instead, string still refers to an ANSI type. So, the correct declaration remains the same in all versions:

    const
      IsamFileNameLen = 64;
    
    type
      IsamFileName = string[IsamFileNameLen];
    

    This is described in Delphi's documentation:

    https://docwiki.embarcadero.com/RADStudio/en/String_Types_(Delphi)

    The Delphi language supports short-string types - in effect, subtypes of ShortString - whose maximum length is anywhere from 0 to 255 characters. These are denoted by a bracketed numeral appended to the reserved word string. For example:

    var MyString: string[100];
    

    creates a variable called MyString, whose maximum length is 100 characters. This is equivalent to the declarations:

    type CString = string[100];
    var MyString: CString;