0

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!

user3161924
  • 1,849
  • 18
  • 33
  • No, actually the correct type is `string[IsamFileNameLen] `. `ShortString` is a pre-existing alias for `string[255]` – Remy Lebeau Feb 10 '23 at 04:11
  • Almost certainly you are going about the entire project wrongly. – David Heffernan Feb 10 '23 at 05:04
  • 1
    Someone tried to port it to XE2, see this attempt: https://stackoverflow.com/questions/8837159/turbopower-b-tree-filer-and-delphi-xe2-anyone-done-it – malom Feb 10 '23 at 09:26

1 Answers1

3

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;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • It looks like I should be using ShortString for all the other strings too since I'm converting this DOS pascal app to Delphi. Somewhere it may reference index [0] for the length. So it automatically knows if you use [] that the string is an ansistring or does it know it's a ShortString since [0] I guess is different. – user3161924 Feb 10 '23 at 04:17
  • The compiler knows the string type you are using. It treats `[]` depending on the real type. – Remy Lebeau Feb 10 '23 at 04:25
  • My worry is if I use `ansistring` where they were using `string` for DOS version, they may have used [0] to get/set the length of the string since that how it was with that version. Does `ansistring[0]` emulate a `shortstring[0]` for length information? – user3161924 Feb 10 '23 at 04:31
  • Oh nice, I see they did use [0] and the compiler picked it up and said `Element 0 inaccessible - use 'Length' or 'SetLength'` so I'll be able to do it either way, I think I'll stick with AnsiString and handle issues as compiler finds it. – user3161924 Feb 10 '23 at 04:35
  • "*My worry is if I use `ansistring` where they were using `string` for DOS version, they may have used `[0]` to get/set the length of the string*" - they would only have done that if `string` were `ShortString` not `AnsiString` (ie [`{$H-}`](https://docwiki.embarcadero.com/RADStudio/en/Long_strings_(Delphi)) in pre-2009 versions of Delphi). "*Does `ansistring[0]` emulate a `shortstring[0]` for length information?*" - no. – Remy Lebeau Feb 10 '23 at 04:39