0
votes

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)

1
Yes, that's correct. Fixed length string is a sort of ShortString 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
@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? - user1556433
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
@DavidHeffernan - These varibles have fixed length as per database columns length. I want to maintain the length of string but in unicode way. - user1556433

1 Answers

4
votes

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.