1

I am migrating a delphi 7 application to delphi XE4. In Delphi 7, some variables are declared like this:

var abc : string[80];

While migrating this code, I am changing above code declaration as

var abc : string;

As per my understanding, string[80] is ansistring and string is unicode. So, is it right way to do it?

I am following the below link from stackoverflow:

Convert Char into AnsiChar or WideChar (Delphi)

Community
  • 1
  • 1
  • 3
    Yes, that's correct. Fixed length string is a sort of [`ShortString`](http://docwiki.embarcadero.com/RADStudio/XE/en/String_Types#Short_Strings) which is a single byte char type. I'd say for your application will be better to have all the strings in the Unicode form rather than ANSI, so if I were you, I'd use `array[0..79] of Char` or just `string`. – TLama Sep 10 '13 at 06:03
  • @TLama - Thanks for clearing my doubt. Also one more question related to it. Shortstring is also used in many placed in delphi 7 code. Should I replace those declarations with string in Delphi XE4? –  Sep 10 '13 at 06:08
  • Some additional references: http://docwiki.embarcadero.com/RADStudio/XE4/en/Unicode_in_RAD_Studio – bummi Sep 10 '13 at 06:12
  • Generally you should use string. But you don't tell us why the old code used shortstring. That's been bad since Delphi 2. What does the code do with these variables? How can we give sound advice without the full picture. – David Heffernan Sep 10 '13 at 06:16
  • @DavidHeffernan - These varibles have fixed length as per database columns length. I want to maintain the length of string but in unicode way. –  Sep 10 '13 at 06:21
  • You can use string. I expect that the db layer will truncate the strings so you may need to do nothing more in your code. – David Heffernan Sep 10 '13 at 06:30
  • Note that code such as `if abc[1] = 'X' then` is always safe to use with string[80], but may need to be replaced with `if (abc <> '') and (abc[1] = 'X') then` to avoid access violations when trying to read the first character of an empty string. – Gerry Coll Sep 10 '13 at 12:09

1 Answers1

4

Indeed you are correct:

  • string[#] are subtypes of ShortString.
    It has a maximum of 255 characters (depending on the #), and the encoding is undetermined (i.e. up to you).
  • string is a regular string, which was single-byte (now called AnsiString) up until Delphi 2007, and multi-byte (now called UnicodeString) as of Delphi 2009.
    Until Delphi 2007, the encoding is undetermined. As of Delphi 2009, both AnsiString and UnicodeString can have an encoding.

More background information can be found in these two Delphi documentation topics:

Answering your question about how you should replace ShortString:

It totally depends on how you used your ShortString in Delphi 7. Depending on the use, there are multiple ways to go:

  • string
  • arrays of byte
  • AnsiString

That all depends on the kind of data you are storing, so that is the first thing you need to find out.

Jeroen Wiert Pluimers
  • 23,965
  • 9
  • 74
  • 154
  • Not quite true. string[17] is only 18 bytes in size and can not hold more than 17 AnsiChars and a length byte. Shortstring is in fact the same as string[255]. Otherwise +1. – Rudy Velthuis Sep 10 '13 at 09:27
  • 2
    There is a crucial difference not mentioned yet: ShortString is a value type whereas String is a reference type -> you cannot use String type as a part of persistent data without additional serialization/deserialization, but ShortString you can. Hence the major criterium is a data usage. – pf1957 Sep 10 '13 at 18:59